Selenium 2 Examples

Posted: December 11th, 2009 | Author: | Filed under: Examples | Tags: , , , | 15 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.


15 Comments on “Selenium 2 Examples”

  1. 1 Gergely said at 10:13 am on April 26th, 2010:

    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.

  2. 2 Dave said at 3:24 pm on April 26th, 2010:

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

  3. 3 Gergely said at 9:20 am on April 27th, 2010:

    Ahh i understand now! Thanks very much for the explanation!

  4. 4 Shanmugavel said at 1:11 pm on October 19th, 2010:

    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

  5. 5 JoseDev said at 6:21 pm on March 16th, 2011:

    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

  6. 6 Dave said at 1:38 pm on March 17th, 2011:

    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.

  7. 7 Jovi said at 2:56 am on March 20th, 2011:

    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

  8. 8 Ricky said at 2:45 pm on June 14th, 2011:

    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.

  9. 9 Dave said at 12:38 am on June 17th, 2011:

    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

  10. 10 anbarasi said at 6:26 am on March 21st, 2012:

    can anyone tell me how to run selenium ide testcases with web driver in php

  11. 11 Dave said at 10:31 am on March 22nd, 2012:

    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/

  12. 12 Mal said at 6:40 pm on July 2nd, 2012:

    I’m using Firefox webdriver on windows 7. I’m trying to click on some element on page. I have Xpath which locates element on selenium IDE but when I run on webdriver it throws exception.
    Same happens with CSS locators as well.

  13. 13 Snehal Masne said at 11:29 am on September 1st, 2012:

    Hi,

    Thanks for the tutorial. :)

    I am trying few more examples. The issue that I found is Selenium works unpredictably. Its kind of dependent on the time taken by website to load. Also JavaScript in ‘executeScript’ and actual selenium scripts are not equivalent.

    Could you please provide tutorial on working with GWT components ? I am facing a lot of problems while automating the flows.

    Regards,
    Snehal Masne

  14. 14 kenneth said at 2:57 am on January 18th, 2013:

    What’s the diffrences bettween driver.close() and driver.quit()? If the driver gets a URL and opens a new tab and then invokes the close() method, what would happens? I tired, but not satified with it. Thanks.

  15. 15 Dave said at 7:27 pm on March 12th, 2013:

    From the JavaDoc:

    • close: Close the current window, quitting the browser if it’s the last window currently open.
    • quit: Quits this driver, closing every associated window.

Leave a Reply