2012-12-19

How to manage User Agents in JMeter

In this following article we are going to see how we can manage different type of user agents to simulate variety browser's hit on a server for load/stress testing. I am using jmeter 2.8.

What is user agent? In short, User Agent is an identity of a client (user). A user agent has details about the client such as OS and its version, browser name and its version. Server processes the requests and provides responses for your browser by identifying using User Agent. User Agent string resides in the header section of each reques
For more detail, please follow wiki. I will try to provide a separate post on user agent.

So, Lets start. 
-First start Jmeter and add a Thread group(10 users, 5 seconds) [For basic idea on jmeter ,see my previous post1 and post2]

-Add a HTTP Request Defaults under Thread group.(I have config the site as www.kaz.com.bd)

-Add a HTTP Header Manager under Thread group

A HTTP Header Manager consists the headers which will be sent along with the request to the server. It usually contains information such as Accept Language, Accept, User Agent, and Accept Encoding.

-Click Add on the bottom of HTTP Header Manager.[A row will be added]

-Click to edit column Name: as User-agent and  column value as
Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25 
I am using this string as Safari 6.0 user agent.
 
If you use Firefox 16.0.1 The string will be

Mozilla/6.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1
Or Internet Explorer 10.6 The string will be
Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0

A full list of different user agents text string is present in this link and this link

-Add a Loop controller [this is a Logic controller] under Thread group(select forever )

-Add a Http request [This is a Sampler]under loop controller and provide path.(i use /index.html for home path on KAZ's site)

-Add View Results Tree under Thread group
-Now, Run the thread and see RAW and HTTP Request from View Results Tree.






We can see the user agent string that is passed through http header.

Note :
-Only one user agent can be used in HTTP Header Manager which is under thread group.
-If any other HTTP Header Managers are under other http request, remove/delete/disable them. Without this, main HTTP Header Manager will not work.
-In this example , the main focus is on user agent changing based on http header manager. Other detail descriptions are kept small.

.... Thanks....:) 

2012-12-18

Actions in selenium webdriver

In this article we are going to see how many type of action can be perform in selenium web driver. I am using C#(VS 2010). We will see different methods to perform action.

Preliminary ideas is , every new action needs to build and then it will be ready to perform. That means, in most of the cases, we will use .Build() and then .perform() after initiation new action.
Actions Class is responsible for every action performed in browser. It is under namespace OpenQA.Selenium.Interactions.
The default constructor :
Actions(driver); //driver is the objcet of any browser driver[driver = new FirefoxDriver();]

Actions that can be performed by Action Class : 
-To Build a sequence of actions and that returns IAction
new Actions(driver).Build();
-To Perform the currently built action and no return
new Actions(driver).Perform();
-To Perform specific action on the browser and no return
new Actions(driver).Build().Perform();//better to specify by finding element
-To Click mouse at the last known mouse coordinates and that returns Actions
new Actions(driver).Click();
 -To Click the mouse on the specified element and that returns Actions
new Actions(driver).Click(draggable);
-To Click and hold the mouse button at the last known mouse coordinates  and that returns Actions
new Actions(driver).ClickAndHold();
-To Click and hold the mouse button down on the specified element and that returns Actions
new Actions(driver).ClickAndHold(draggable);
-To Right-click the mouse at the last known mouse coordinates and that returns Actions.
new Actions(driver).ContextClick();
-To Right-click the mouse on the specified element and that returns Actions.
new Actions(driver).ContextClick(draggable);
-To Double-click the mouse at the last known mouse coordinates and that returns Actions.
new Actions(driver).DoubleClick();
-To Double-click the mouse on the specified element and that returns Actions.
new Actions(driver).DoubleClick(draggable);
-To drag-and-drop from one element to another and that returns Actions.
new Actions(driver).DragAndDrop(draggable, droppable);
-To drag-and-drop on one element to a specified offset and that returns Actions.
new Actions(driver).DragAndDropToOffset(draggable, 25, 35);
-To Send a modifier key down message to the browser and that returns Actions.
new Actions(driver).KeyDown(Keys.Alt);//key parameter as string
-To Send a modifier key down message to the specified element in the browser and that returns Actions.
new Actions(driver).KeyDown(draggable, Keys.Alt);//key parameter as string
-To Send a modifier key up message to the browser and that returns Actions.
new Actions(driver).KeyUp(Keys.Alt);//key parameter as string
-To Send a modifier up down message to the specified element in the browser and that returns Actions.
new Actions(driver).KeyUp(draggable, Keys.Alt);//key parameter as string
-To Move the mouse to the specified offset of the last known mouse coordinates and that returns Actions.
new Actions(driver).MoveByOffset(25, 35);//(Xoffset, Yoffset)
-To Move the mouse to the specified element and that returns Actions.
new Actions(driver).MoveToElement(draggable);
-To Move the mouse to the specified offset of the top-left corner of the specified element and that returns Actions.
new Actions(driver).MoveToElement(draggable, 25, 35);//(Xoffset, Yoffset)
-To Release the mouse button at the last known mouse coordinates and that returns Actions.
new Actions(driver).Release();
-To Release the mouse button on the specified element and that returns Actions.
new Actions(driver).Release(draggable);
-To Send a sequence of keystrokes to the browser and that returns Actions.
new Actions(driver).SendKeys(Keys.Alt);//key parameter as string
-To Send a sequence of keystrokes to the specified element in the browser and that returns Actions.
new Actions(driver).SendKeys(draggable,Keys.Alt);//key parameter as string

Note : Here draggable and droppable are two web element(IWebElement type). All action I used with new(to perform separately). Returns Actions means, the function return Action type object. When necessary, we can use this.

...Thanks...:) I will try to use those in my examples in future posts.

How to drag and drop in Selenium webdriver

In this article, we are going to see how we can drag and drop an web element with JQuery UI.  I am using c# with VS 2010.

To drag and drop, first, we have to initiate two web elements which should be draggable and droppable . And then using those two web elements together we can perform an action drag and drop. So initiations will be define by driver finding elements. In code , it will be
private IWebElement draggable;
private IWebElement droppable;
In the test method, we have to declear them
draggable = driver.FindElement(By.Id("draggableIDstring"));//we can use other method like Xpath to find element, for this, see from my old posts.
droppable = driver.FindElement(By.Id("droppableIDstring"));
Then we have to perform a new action with those web elements.
new Actions(driver).DragAndDrop(draggable, droppable).Build().Perform();

In here, Actions initiated by driver, performs(after build) drag & drop.
Note: Be careful of building the new action before perform[.Build()]. Here draggable is source element and droppable is destination element.

I will try to provide more action performing.

Thanks...:)

How to press Keyboard in selenium webdriver?

In this article we are going to see the functions for keyboard's keypass key events. I am using c# in vs2010.
Before starting , please follow my previous posts for initial setup.

To find an element we use
driver.FindElement(By.XPath("String"));
if we add send keys as keyboard command, then we can use keyboard event with the element that we found. like
driver.FindElement(By.XPath("String")).SendKeys(Keys.ArrowRight);
Here, "string" is the Xpath loaction. And on the element that we have found, keyboard's Arrow left will be pressed.

Like that following keys can be pressed
-For the number pad addition key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Add);
-For  the Alt key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Alt);
-For  the Left arrow key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.ArrowDown);
-For  the left arrow key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.ArrowLeft);
-For  the right arrow key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.ArrowRight);
-For  the up arrow key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.ArrowUp);
-For  the Backspace key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Backspace);
-For  the Cancel keystroke.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Cancel);
-For  the Clear keystroke.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Clear);
-For  the function key COMMAND.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Command);
-For  the Control key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Control);
-For  the number pad decimal separator key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Decimal);
-For  the Delete key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Delete);
-For  the number pad division key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Divide);
-For  the Left arrow key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Down);
-For  the End key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.End);
-For  the Enter key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Enter);
-For  the equal sign key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Equal);
-For  the Escape key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Escape);
-For  the function key F1.
driver.FindElement(By.XPath("String")).SendKeys(Keys.F1);
-For  the function key F10.
driver.FindElement(By.XPath("String")).SendKeys(Keys.F10);
-For  the function key F11.
driver.FindElement(By.XPath("String")).SendKeys(Keys.F11);
-For  the function key F12.
driver.FindElement(By.XPath("String")).SendKeys(Keys.F12);
-For  the function key F2.
driver.FindElement(By.XPath("String")).SendKeys(Keys.F2);
-For  the function key F3.
driver.FindElement(By.XPath("String")).SendKeys(Keys.F3);
-For  the function key F4.
driver.FindElement(By.XPath("String")).SendKeys(Keys.F4);
-For  the function key F5.
driver.FindElement(By.XPath("String")).SendKeys(Keys.F5);
-For  the function key F6.
driver.FindElement(By.XPath("String")).SendKeys(Keys.F6);
-For  the function key F7.
driver.FindElement(By.XPath("String")).SendKeys(Keys.F7);
-For  the function key F8.
driver.FindElement(By.XPath("String")).SendKeys(Keys.F8);
-For  the function key F9.
driver.FindElement(By.XPath("String")).SendKeys(Keys.F9);
-For  the Help keystroke.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Help);
-For  the Home key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Home);
-For  the Insert key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Insert);
-For  the left arrow key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Left);
-For  the Alt key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.LeftAlt);
-For  the Control key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.LeftControl);
-For  the Shift key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.LeftShift);
-For  the function key META.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Meta);
-For  the number pad multiplication key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Multiply);
-For  the NUL keystroke.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Null);
-For  the number pad 0 key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.NumberPad0);
-For  the number pad 1 key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.NumberPad1);
-For  the number pad 2 key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.NumberPad2);
-For  the number pad 3 key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.NumberPad3);
-For  the number pad 4 key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.NumberPad4);
-For  the number pad 5 key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.NumberPad5);
-For  the number pad 6 key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.NumberPad6);
-For  the number pad 7 key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.NumberPad7);
-For  the number pad 8 key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.NumberPad8);
-For  the number pad 9 key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.NumberPad9);
-For  the Page Down key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.PageDown);
-For  the Page Up key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.PageUp);
-For  the Pause key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Pause);
-For  the Return key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Return);
-For  the right arrow key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Right);
-For  the semi-colon key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Semicolon);
-For  the number pad thousands separator key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Separator);
-For  the Shift key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Shift);
-For  the Space bar key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Space);
-For  the number pad subtraction key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Subtract);
-For  the Tab key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Tab);
-For  the up arrow key.
driver.FindElement(By.XPath("String")).SendKeys(Keys.Up);

For multiple key press :
First, get the ascii code for the key. For example if we want to press Ctrl-A, it should be 0001. The full list is here (or here). First assign a character(c) to the value and then convert that as string inside send key.
char c = '\u0001'; // ASCII code 1 for Ctrl-A
driver.FindElement(By.XPath("String")).SendKeys(Convert.ToString(c));


---------------Another way works in Java---------------
Make a action instance and use action instance . Inhere, we have used 3type of method to press three different key , and as they are different from each other, it will make 3 key press together.(means in here Alt+Shift+T)
Actions kpress = new Actions(driver);
kpress.keyDown(driver.findElement(By.id("name")), Keys.ALT).perform(); kpress.sendKeys(driver.findElement(By.id("name")), "T").perform();
kpress.keyUp(driver.findElement(By.id("name")),Keys.SHIFT).perform();



...Thanks....:)...

How to do R&D effectively in kan-ban(Agile)