Sunday, October 28, 2012

Convert Selenium 1 RemoteControl to Selenium 2 WebDriver




Here is a more complete reference guide to converting Java Remote Control commands into Selenium 2 WebDriver Java commands. This comes straight out of the class we teach at LMN Solutions. Selenium 2 classes in Reston, VA

Description
Remote Control – Selenium 1
WebDriver - Selenium 2 Command
Get access to driver
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.w3schools.com/");
selenium.start();
FirefoxDriver driver = new FireFoxDriver(); 
InternetExplorerDriver driver = new InternetExplorerDriver();
HtmlUnitDriver driver = new HtmlUnitDriver();
OperaDriver driver = new OperaDriver();
ChromeDriver driver = new ChromeDriver();
Page Title
selenium.getTitle();
driver.getTitle();
Navigate to page
selenium.open(“http://www.google.com”);
driver.get(“http://www.google.com”);
Find element by name

element = driver.findElement(By.name("first_name"));
Delete text from input field
selenium.type("");
element.clear();
Sets value of input field
selenium.type("Brad");
element.sendKeys("Brad");
Click on the element

selenium.click("submit");
element.click();
Note: If element is a link or button it will perform the click action
Return text between element tags
selenium.getText("//h3[2]");
element.getText(); 
Returns the value of the element
selenium.getValue("first_name");
element.getValue();
Returns true or false designating if the element is selected
selenium.isChecked("female");
element.isSelected();
Get the Select list value
selenium.getSelectedValue("cars");
Select select = new Select(element);
select.getFirstSelectedOption();
Where on the page is the top left-hand corner of the rendered element
n/a
element.getLocation();
Get the tag name of the element
n/a
element.getTagName();
Returns true or false designating if the element is displayed
selenium.isVisible("hidden_field");
element.isDisplayed();
Returns true or false designating if the element is enabled.  Will generally return true for everything but disabled input elements
selenium.isEditable("last_name");
element.isEnabled();
Width and height of the rendered element
Almost equivalent
selenium.getAttribute("first_name@maxlength");
element.getSize();
Returns the attribute
<input type="textvalue="Joename="FirstName">
selenium.getAttribute("FirstName@value");
element.getAttribute(“value”);
Submits the form if element is a form or within a form
selenium.submit("first_name");
element.submit();
Alerts
selenium.chooseOkOnNextConfirmation();
selenium.chooseCancelOnNextConfirmation();
Alert alert = driver.switchTo().alert();
alert.accept();
alert.dismiss();
Frames
selenium.selectFrame("relative=up");
selenium.selectFrame("showframe");
driver.switchTo.defaultContent();
driver.switchTo.frame("showframe);
Double Click
selenium.doubleClick("aButton");
Actions action = new Actions(driver);
action.doubleClick(addElement);
Element Present
selenium.isElementPresent();
Try {
   element = driver.findElement(By.name("name"));
} catch (NoSuchElementException e) {
   fail("name - no such element");
}


© 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

Wednesday, September 19, 2012

How to run Selenium WebDriver with Console Output


How to run Selenium WebDriver with Console Output

I have been asked how to run Selenium 2 WebDriver in a mode that emulates the console output window that Selenium Remote Control has. WebDriver starts up the selenium server in the background and you cannot see any error messages that might be helpful.

First start up the selenium console as you did with Selenium 1 tests:

java -jar selenium-server-standalone.x.xx.x.jar

In your test code use this:


driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),   DesiredCapabilities.firefox());

instead of this:
        
driver = new FirefoxDriver();

but of course you need to import this:


import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

So you will get the helpful logging that Selenium provides below:






© 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

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