Sunday, March 11, 2012

Selenium 1 to Selenium 2 WebDriver part 2

One important difference between Selenium 1 and WebDriver in Selenium 2 is that finding the element is a separate command from performing an action on the element.

You also need to be careful about trying to reuse a WebElement object after a page is reloaded. You must find the element again after a page refresh.

Here is an example of selecting a checkbox and submitting a form.


Testing the page http://www.w3schools.com/Html/tryhtml_form_checkbox.htm

Selenium 1


verifyTrue(selenium.isElementPresent("input"));
selenium.click("vehicle");
verifyEquals("on", selenium.getValue("vehicle"));
selenium.click("css=input[value='Submit']");


Selenium 2


driver.get("http://www.w3schools.com/Html/tryhtml_form_checkbox.htm");
List<WebElement> elements = driver.findElements(By.name("vehicle"));
if (elements.size() == 0) fail("vehicle not found");
WebElement element = driver.findElement(By.cssSelector("input[value='Car']"));
element.click();
assertTrue(element.isSelected());

element.submit();

© Copyright 2012 LMN Solutions

If you  have a question e-mail us or add a comment.






LMN Solutions - Find out more about us.
Selenium Training

Saturday, March 3, 2012

Selenese to WebDriver - Moving from Selenium 1 to Selenium 2

Once you know Selenium 1 commands called Selenese it is not that hard to convert to Selenium 2 with WebDriver. If you have a few hundred test cases it may take a while to convert over, but it is worth it.

WebDriver is much more stable than the old way and it is much easier to get a handle on those pesky unnamed pop up windows, frames and iframes the developers might throw at you.

This is the start of a series of blogs containing examples of the differences between WebDriver and Selenese commands.

Test Setup Difference


Selenium 1


selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.w3schools.com/");
selenium.start();


Selenium 2


driver = new InternetExplorerDriver();
driver.get("http://www.w3schools.com/Html/tryhtml_form_submit.htm");


Locator and Verification Separation

With WebDriver the act of locating and element is separate from getting the value from the element.

Selenium 1


selenium.open("/Html/tryhtml_form_submit.htm");


verifyEquals("First name: \n Last name:", selenium.getText("input"));


Selenium 2 with WebDriver

WebElement element = driver.findElement(By.name("input"));
verifyEquals("First name:\nLast name:", element.getText());

And that is just the start. More to come with forms and frames and windows.

© Copyright 2012 LMN Solutions
If you  have a question e-mail us or add a comment.






LMN Solutions - Find out more about us.
Selenium Training