Selenium 2 Examples
Posted: December 11th, 2009 | Author: Dave | Filed under: Examples | Tags: example, new release, selenium 2, webdriver | 11 Comments »Yesterday Selenium 2 (alpha 1) was released. This is the first release since the Selenium and WebDriver projects started to merge. The main difference is the inclusion of the WebDriver API into Selenium. I’ve put together a small example below that uses the new API to log into two web based e-mail clients and send an e-mail.
WebDriverTestBase.java
package tests; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Wait; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; public class WebDriverTestBase { public static FirefoxDriver driver; public static Wait wait; @BeforeClass(alwaysRun = true) protected void startWebDriver() { driver = new FirefoxDriver(); wait = new WebDriverWait(driver, 120); } @AfterClass(alwaysRun = true) protected void closeSession() { driver.close(); } public static void assertEquals(Object actual, Object expected) { Assert.assertEquals(actual, expected); } }
VisibilityOfElementLocated.java
package tests; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.support.ui.ExpectedCondition; public class VisibilityOfElementLocated implements ExpectedCondition { By findCondition; VisibilityOfElementLocated(By by) { this.findCondition = by; } public Boolean apply(WebDriver driver) { driver.findElement(this.findCondition); return Boolean.valueOf(true); } }
WebmailTest.java
package tests; import org.openqa.selenium.By; import org.testng.annotations.Test; public class WebmailTest extends WebDriverTestBase { //variables public static final String YAHOO_EMAIL = "example@yahoo.co.uk"; public static final String HOTMAIL_EMAIL = "example@hotmail.co.uk"; @Test(description = "Sends an e-mail from Yahoo account") public void sendFromYahoo() { //new message variables String to = HOTMAIL_EMAIL; String subject = "Test Sending Email Message From Yahoo"; String message = "This is a test e-mail from Yahoo"; //login to yahoo driver.get("http://mail.yahoo.com/"); driver.findElement(By.id("username")).sendKeys(YAHOO_EMAIL); driver.findElement(By.id("passwd")).sendKeys("mytestpw"); driver.findElement(By.id(".save")).click(); //create new message driver.findElement(By.id("compose_button_label")).click(); wait.until(new VisibilityOfElementLocated(By.xpath("id('_testTo_label')/ancestor::tr[1]//textarea"))); //send test message driver.findElement(By.xpath("id('_testTo_label')/ancestor::tr[1]//textarea")).sendKeys(to); driver.findElement(By.xpath("id('_testSubject_label')/ancestor::tr[1]//input")).sendKeys(subject); driver.switchTo().frame("compArea_test_"); driver.findElement(By.xpath("//div")).sendKeys(message); driver.switchTo().defaultContent(); driver.findElement(By.id("SendMessageButton_label")).click(); //WARNING! sometimes a captcha is displayed here wait.until(new VisibilityOfElementLocated(By.xpath("//nobr[contains(text(), 'Message Sent')]"))); } @Test(description = "Sends an e-mail from Hotmail account") public void sendFromHotmail() { //new message variables String to = YAHOO_EMAIL; String subject = "Test Sending Email Message From Hotmail"; String message = "This is a test e-mail from Hotmail"; //login to hotmail driver.get("http://mail.live.com/"); driver.findElement(By.name("login")).sendKeys(HOTMAIL_EMAIL); driver.findElement(By.name("passwd")).sendKeys("mytestpw"); if (driver.findElement(By.name("remMe")).isSelected()) { driver.findElement(By.name("remMe")).click(); } driver.findElement(By.name("SI")).click(); //create new message driver.switchTo().frame("UIFrame"); driver.findElement(By.id("NewMessage")).click(); //send test message driver.findElement(By.id("AutoCompleteTo$InputBox")).sendKeys(to); driver.findElement(By.id("fSubject")).sendKeys(subject); driver.switchTo().frame("UIFrame.1"); driver.findElement(By.xpath("//body")).sendKeys(message); driver.switchTo().frame("UIFrame"); driver.findElement(By.id("SendMessage")).click(); assertEquals(driver.findElement(By.cssSelector("h1.SmcHeaderColor")).getText(), "Your message has been sent"); } }
Disclaimer: These tests are working at the time of this post but do require active e-mail accounts. They are likely to fail when the web based e-mail clients update their applications. Also, please don’t misuse these examples – their intention is to show how to use the WebDriver API and were inspired by a test assignment for an interview process.
Hi!
I just gone over the code once more and there is a part i don’t understand..
public Boolean apply(WebDriver driver) {
driver.findElement(this.findCondition);
return Boolean.valueOf(true);
}
Doesn’t this always return true? And i don’t see you using it sort of.. Is that part of the interface and must be implemented?
Thanks,
Gergely.
It will always return true, unless an exception occurs. The findElement method will throw a NoSuchElementException if the element doesn’t exist. This is no longer required in Selenium 2.0a4 as there is support for implicit waits, however these only wait for the element to be present, which means it’s not necessarily visible (or enabled, etc).
Ahh i understand now! Thanks very much for the explanation!
Hi all,
I am trying Webdriver with C# – Visual Studio and i followed instructions…
Finally i can’t listed WebDriver namespace under selenium as below..
using org.openqa.selenium.WebDriver;
Can anybody help me?
Thx,
SCSVEL
I’m trying to test the code to learn how it properly works but y get a compilation error in VisibilityOfElementLocated class since its suposed to implement the apply func as public Object aply(Object t). how can I fix this? Cheers
The VisibilityOfElementLocated is no longer needed as the latest version on Selenium 2 (currently 2.0b2) supports implicit waits. With implicit waits, you only need to use one of the findElement methods, and if the target element does not exist or is not visible, Selenium will wait until it is. Unfortunately this example is out of date, however if you look at some of my more recent examples, hopefully you will find them to be working.
Hi Dave,
Thanks for posting these example, here I have a question. In the 1.0, we used to pass in the find element locator with the every type we want (xpath, id, link text, css ,etc), and the api such as click, type, etc can handle them transparently like they do not care the type. And now in 2.0, looks like we will use findElement via WebDriver, and pass in a specified locator type such as by.id, by.name, etc. I know that’s a enhancement in 2.0 to locate the web element, but based on my custom framework, how can I build one method to deal with the different type we pass in.
Thanks,
-Jovi
Hi, I tried to run this script but this script types the password in the username field. Could you please verify whether it runs perfectly? Thanks.
Hi Ricky,
This is an old post and I suspect both web applications used in the example have seen multiple releases since. I would use this purely as an example and not attempt to run it.
Thanks,
Dave
can anyone tell me how to run selenium ide testcases with web driver in php
I’m not sure about the status of WebDriver formatters for PHP, but the PHP formatters are available here: https://addons.mozilla.org/en-US/firefox/addon/selenium-ide-php-formatters/