Aim:
This example will demonstrate the Page Object Model technique which allows for better script maintenance and reduces code duplication. This type of framework allows the scripts to be less "brittle"
Difficulty Level:Medium
Prerequisites:
Also See:
http://code.google.com/p/selenium/wiki/PageObjects
http://stackoverflow.com/questions/466819/how-can-i-make-my-selenium-tests-less-brittle
http://stackoverflow.com/questions/7997858/page-object-model-best-practices-in-selenium
What did you learn?
Scripts:
Save the code as DisplayNewsTest.java
Save the code as LoginPage.java
Save as PostPage.java
Save as AddUser.java
Helpful Video:
This example will demonstrate the Page Object Model technique which allows for better script maintenance and reduces code duplication. This type of framework allows the scripts to be less "brittle"
Difficulty Level:Medium
Prerequisites:
- Eclipse IDE installed
- Selenium 2/Webdriver API.
- Driver.manage()
- Driver.get(baseUrl)
- Driver.findElement by id, name, linkText, css Selector and xpath
- clear()
- sendkeys()
- click()
- random string
- Download and Install Eclipse IDE using link http://www.eclipse.org/downloads/
- Tip: download the version suitable for your PC ( 32 bit/64bit))
- Download the Selenium2 API 'selenium-server-standalone-*.jar' library using link http://seleniumhq.org/download
- No need to get JUnit jar file in separate as it is inbuilt in 'selenium-server-standalone-*.jar'.
- Launch the Eclipse IDE
- Create a new Java project and name it FirstExample. You will see a default folder 'src' inside it.
- Do right click on the created project and select Build Path >> Configuration Build Path
- Add 'selenium-server-standalone-*.jar' under Libraries tab by clicking 'Add External Jars'.
- Right click on 'src' and add a package 'com.example.tests'.
- Create 4 java classes 'postPage.java', ' AddUser.java', 'loginPage.java' and 'DisplayNewsTest.java' into the created package.
- Paste the below mentioned 4 scripts in corresponding java files.
- Right click 'DisplayNewsTest.java' and click 'Run as >> JUnit test'.
Also See:
http://code.google.com/p/selenium/wiki/PageObjects
http://stackoverflow.com/questions/466819/how-can-i-make-my-selenium-tests-less-brittle
http://stackoverflow.com/questions/7997858/page-object-model-best-practices-in-selenium
What did you learn?
- the usage of page object modelling ( Page object model) framework
- makes you think about script maintenance
Scripts:
Save the code as DisplayNewsTest.java
package com.example.tests;
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class DisplayNewsTest {
private WebDriver driver;
public 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 testDisplayNewsTest() throws Exception {
LoginPage loginPage = new LoginPage(driver);
PostPage dashboard = loginPage.loginAs("admin", "time4test");
AddUser newsPage = dashboard.goToNewPage();
newsPage.NewDisplayed();
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
}
Save the code as LoginPage.java
package com.example.tests;
import org.openqa.selenium.*;
public class LoginPage {
private WebDriver driver;
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public PostPage loginAs(String userName,String password) {
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(userName);
driver.findElement(By.id("user_pass")).clear();
driver.findElement(By.id("user_pass")).sendKeys(password);
driver.findElement(By.id("wp-submit")).click();
return new PostPage(driver);
}
}
Save as PostPage.java
package com.example.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class PostPage {
private WebDriver driver;
public PostPage(WebDriver driver) {
this.driver = driver;
}
public AddUser goToNewPage() {
driver.findElement(By.linkText("Posts")).click();
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();
return new AddUser(driver);
}
}
Save as AddUser.java
package com.example.tests;
import org.openqa.selenium.By;
import java.util.*;
import org.openqa.selenium.WebDriver;
public class AddUser {
private WebDriver driver;
public AddUser(WebDriver driver) {
this.driver = driver;
}
public void NewDisplayed()throws Exception {
driver.findElement(By.linkText("Users")).click();
driver.findElement(By.xpath("//li[@id='menu-users']/div[3]/ul/li[2]/a")).click();
Random rand = new Random();
int numNoRange = rand.nextInt();
String userstr="TestNewUser"+numNoRange;
String email="Testemail"+numNoRange+"@yahoo.in";
driver.findElement(By.id("user_login")).clear();
driver.findElement(By.id("user_login")).sendKeys(userstr);
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys(email);
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:
I think the code in line 15 of AddUser.java file is not working. I get an error that the given selector is invalid.
ReplyDeleteI presume it searches for the Add User button on the dashboard ? How can we change this ?
Also can we randomise the usernames that the script creates? Something using the timestamp perhaps?
Actually I don't get the error anymore.
ReplyDeleteBut a random username generator would be nice.
hmmmmm....I do get the error message again. Strange but it worked once!
ReplyDeleteAnyway definetely need to fix line 15 as mentioned above.
Its done....
ReplyDeleteVipin,
ReplyDeleteThis example seems to need Junit library, but not mentioned in pre requisites. Can you please add in the aforementioned list ?
Thanks.
In this example we are using Selenium 2(Webdriver) API.So there is no need of JUnit library.Junit library is alraedy inbuilt in Selenium2.
ReplyDeleteTip:the file names are case sensitive!
ReplyDeletehe basic idea is that you use the Page Object Model to test the logical flow between the pages of a web app, instead of just testing chunks from a page (i.e. some buttons or the presence of some links). It is used to chain actions and help the tester see how transition is made between the states of a web app.
ReplyDeleteFor example: You are on page A -> you do some actions -> you click submit -> you expect to be on page B -> you then do some actions on that page (B) -> you click submit -> you are on page C -> you do some invalid actions on that page (C) -> you click submit -> you expect to remain on page C...
And so on, and so forth...
http://www.f14testing.com/archives/1532
ReplyDelete