Sunday, August 19, 2012

Selenium Commands – “Selenese”


ActionsAccessors, and Assertions.
}Actions are commands that generally manipulate the state of the application. They do things like “click this link” and “select that option”. If an Action fails, or has an error, the execution of the current test is stopped.
Øselenium.click("id=gbztms");
Øselenium.select("id=citytravel_city", "label=Bangalore");
}Accessors examine the state of the application and store the results in variables, e.g. “storeTitle”. They are also used to automatically generate Assertions.
ØString Google = selenium.getTitle(); 
Øselenium.type("id=gbqfq", Google);
}Assertions are like Accessors, but they verify that the state of the application conforms to what is expected. Examples include “make sure the page title is X” and “verify that this checkbox is checked”.
ØassertTrue(selenium.isElementPresent("id=lga"));
ØassertEquals(selenium.getTitle(), "Google");

Finding the locators using selenium


Direct Reference  Using one of the direct references to the HTML element
Id=MyButton (note in this case, “Id =” is not even required)
Name=Description_Field
Value=ABC XYZ
Link=Contact Us

XPath, (XML Path Language)  :xpath, for locators starting with "//"
Reference: http://www.w3schools.com/xpath/default.asp Relatively straightforward to compose and read later down the track.
xpath=//img[@alt='The image alt text']
xpath=//table[@id='table1']//tr[4]/td[2]
xpath=//a[contains(@href,'#id1')]
xpath=//a[contains(@href,'#id1')]/@class
xpath=(//table[@class='stylee'])//th[text()='theHeaderText']/../td
xpath=//input[@name='name2' and @value='yes']
xpath=//*[text()="right"]
/html/body/div[1]/div[5]/div/table/tbody/tr/td/p/a[3] (An example bad XPath – it will break as soon as the page layout changes a bit)


DOM and CSS : 
dom=document.images[0] (the first image in the page)
css=H1.topic (the first heading with the class name of topic) or css=h1.topic[value="Topic Index"] (the heading with the specific text)
dom=document.forms['myForm'].myDropdown
dom=document.images[56]
dom=function foo() { return document.links[1]; }; foo();
css=a[href="#id3"]
css=span#firstChild + span

Keyboard and Coordinates
You can also go to the extreme of KeyDown and KeyUp