this simple example will demonstrate how you can program in Java with Webdriver/Selenium 2 and produce automation scripts. Selenium2/Webdriver offers a rich api set to perform website
Difficulty level
High
What you will learn?
Under this exercise users can learn how to go with web driver having one working example.
Tips
Users should have little bit idea about how to create Junit project over Eclipse IDE, how to run scripts, how to add libraries etc.
Prerequisites:
- Can access Wordpress.com and is familiar with How to works on Wordpress.com?
- Eclipse IDE
- Downloaded Selenium2/Webdriver API from http://seleniumhq.org/download/ ( current version is 2.6.0)
- Download Junit 4.X.jar from http://www.junit.org/
- Add the jar files to Eclipse
Exercise Steps:
1. Launch Eclipse
2. Create a new java project
3. Build project path having the required jar files like selenium, junit, web driver api etc.
4. Attached two java files, save it into the correct package that is referenced into the code itself.
5. Run the tests.
Zip file location: ( download from here)
C:\FoundationSelenium\WebdriverScript\Selenium2Example1.java, C:\FoundationSelenium\WebdriverScript\Selenium2Example2.java.
Solution:
Please go with the video file to view the solution.
Screen shot:
Not Required.
Java Files
First Java File
package com.example.tests;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Selenium2Example1 {
private WebDriver driver;
private String baseUrl="http://foundation.time2test.co.uk/";
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testSelenium2Example1() throws Exception {
driver.get("http://foundation.time2test.co.uk/");
driver.findElement(By.linkText("Log in")).click();
driver.findElement(By.id("user_login")).clear();
driver.findElement(By.id("user_login")).sendKeys("admin");
driver.findElement(By.id("user_pass")).clear();
driver.findElement(By.id("user_pass")).sendKeys("time4test");
driver.findElement(By.id("wp-submit")).click();
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (isElementPresent(By.xpath("//div[@id='user_info']/p/a[2]"))) break; } catch (Exception e) {}
Thread.sleep(1000);
}
driver.findElement(By.linkText("Posts")).click();
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (isElementPresent(By.xpath("//div[@id='wpbody-content']/div[3]/h2"))) break; } catch (Exception e) {}
Thread.sleep(1000);
}
driver.findElement(By.linkText("Add New")).click();
driver.findElement(By.id("title")).clear();
driver.findElement(By.id("title")).sendKeys("MyPost");
driver.findElement(By.id("publish")).click();
Selenium2Example2 se=new Selenium2Example2();
se.testSelenium2Example2(driver);
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}
Second Java File
package com.example.tests;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Selenium2Example2 {
public void testSelenium2Example2(WebDriver driver) throws Exception {
driver.findElement(By.linkText("Users")).click();
driver.findElement(By.xpath("//li[@id='menu-users']/div[3]/ul/li[2]/a")).click();
driver.findElement(By.id("user_login")).clear();
driver.findElement(By.id("user_login")).sendKeys("TestUser");
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys("test123@gmail.com");
driver.findElement(By.id("first_name")).clear();
driver.findElement(By.id("first_name")).sendKeys("TestFirst");
driver.findElement(By.id("last_name")).clear();
driver.findElement(By.id("last_name")).sendKeys("TestLast");
driver.findElement(By.id("url")).clear();
driver.findElement(By.id("url")).sendKeys("http://www.google.com/");
driver.findElement(By.id("pass1")).clear();
driver.findElement(By.id("pass1")).sendKeys("time2test");
driver.findElement(By.id("pass2")).clear();
driver.findElement(By.id("pass2")).sendKeys("time2test");
driver.findElement(By.id("addusersub")).click();
driver.findElement(By.linkText("Log Out")).click();
}
}
Helpful Video
Video step Webdriver 2 Example
1. Open Eclipse IDE
2. Open an existing java Project
src\com\example\test\ Java files
\Selenium2Example1.java
\Selenium2Example2.java
3. Notice the webdriver term used in the java files
4. notice that we have already added the jar file for webdriver
5. Now Run the selenium webdriver test
6. Notice the webdriver script execution and the webdriver label at the foot of the browser
Related Entries:
http://time2testcourse.blogspot.com/2011/05/how-to-easily-create-java-selenium.html
notes for Vipin:
ReplyDeleteuse the VM
create two tests using java
use Selenmium 2/webdriver
use wordpress to test against
keep the example simple
dont show how to instal anything
do put a section on "pre-requsites" and mention wha you need only.
show a video showing eclipse starting, opening up the java files, running the scripts using the java command, show the results, show any logs e.t.c
In progress,
ReplyDeleteit's done..please let me know if anything more is required in same exercise's solution.
ReplyDeletedoes the examaple used here use junit?
ReplyDeleteyes, I have used web driver's APIs in creating the tests and have run the test using Junit.
ReplyDeleteViresh Comments;
ReplyDeletedoes the examaple used here use junit?
I have used Junit in running the scripts only, and have used Web Driver API in creating the scripts that is aimed.
ReplyDeleteViresh Comment:
ReplyDeleteadd the ability to run java files without eclipse IDE.
We should create a framework where:
ReplyDeleteMain Java File - is the master java file that is the Driver and Setup for the individual tests
the other classes/java files are the different tests that need to be executed.
Other steps:
create com.example.tests as per the package file