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 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

selenium@lmnsolutions.com
Leave Yours +

No Comments

Comments are closed.

  • Comments are Closed