Copy all 3 scripts into your selenium2+TestNG environment and run the K7 script
LoginPage:
package PageObject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
public class LoginPage {
final WebDriver driver;
@FindBy(how = How.XPATH, using = ".//*[@id='lgnbox']/div/div[2]/div[2]/form/table/tbody/tr[2]/td/input")
private WebElement usernameEditbox;
@FindBy(how = How.XPATH, using = ".//*[@id='lgnbox']/div/div[2]/div[2]/form/table/tbody/tr[4]/td/input")
private WebElement passwordEditbox;
@FindBy(how = How.XPATH, using = ".//*[@id='lgnbox']/div/div[2]/div[2]/form/table/tbody/tr[5]/td/input")
private WebElement signinButton;
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void enterUsername(String login) {
usernameEditbox.clear();
usernameEditbox.sendKeys(login);
}
public void enterPassword(String password) {
passwordEditbox.clear();
passwordEditbox.sendKeys(password);
}
public void clickSigninButton() {
signinButton.click();
}
public LogOff login(String login, String password) {
enterUsername(login);
enterPassword(password);
clickSigninButton();
return PageFactory.initElements(driver, LogOff.class);
}
}
LogOff:
package PageObject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
public class LogOff {
final WebDriver driver;
public LogOff(WebDriver driver) {
this.driver = driver;
}
@FindBy(how = How.XPATH, using=".//*[@id='pg']/div[1]/div[1]/div[2]/div[4]/ul/li[5]/a")
private WebElement signOff;
public LoginPage singOff(){
signOff.click();
return PageFactory.initElements(driver, LoginPage.class);
}
}
K7:
package PageObject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import PageObject.LogOff;
import PageObject.LoginPage;
public class K7 {
WebDriver driver;
private static String login = "user";
private static String pass = "user@123";
@BeforeClass
public void setUp() throws Exception {
driver = new FirefoxDriver();
driver.get("http://localhost/k7biz/static/index.htm");
}
@Test
public void testLogin() {
LoginPage loginPage = PageFactory.initElements(driver, LoginPage.class);
LogOff findPage = loginPage.login(login, pass);
loginPage = findPage.singOff();
}
@AfterClass
public void tearDown() throws Exception {
driver.quit();
}
}
LoginPage:
package PageObject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
public class LoginPage {
final WebDriver driver;
@FindBy(how = How.XPATH, using = ".//*[@id='lgnbox']/div/div[2]/div[2]/form/table/tbody/tr[2]/td/input")
private WebElement usernameEditbox;
@FindBy(how = How.XPATH, using = ".//*[@id='lgnbox']/div/div[2]/div[2]/form/table/tbody/tr[4]/td/input")
private WebElement passwordEditbox;
@FindBy(how = How.XPATH, using = ".//*[@id='lgnbox']/div/div[2]/div[2]/form/table/tbody/tr[5]/td/input")
private WebElement signinButton;
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void enterUsername(String login) {
usernameEditbox.clear();
usernameEditbox.sendKeys(login);
}
public void enterPassword(String password) {
passwordEditbox.clear();
passwordEditbox.sendKeys(password);
}
public void clickSigninButton() {
signinButton.click();
}
public LogOff login(String login, String password) {
enterUsername(login);
enterPassword(password);
clickSigninButton();
return PageFactory.initElements(driver, LogOff.class);
}
}
LogOff:
package PageObject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
public class LogOff {
final WebDriver driver;
public LogOff(WebDriver driver) {
this.driver = driver;
}
@FindBy(how = How.XPATH, using=".//*[@id='pg']/div[1]/div[1]/div[2]/div[4]/ul/li[5]/a")
private WebElement signOff;
public LoginPage singOff(){
signOff.click();
return PageFactory.initElements(driver, LoginPage.class);
}
}
K7:
package PageObject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import PageObject.LogOff;
import PageObject.LoginPage;
public class K7 {
WebDriver driver;
private static String login = "user";
private static String pass = "user@123";
@BeforeClass
public void setUp() throws Exception {
driver = new FirefoxDriver();
driver.get("http://localhost/k7biz/static/index.htm");
}
@Test
public void testLogin() {
LoginPage loginPage = PageFactory.initElements(driver, LoginPage.class);
LogOff findPage = loginPage.login(login, pass);
loginPage = findPage.singOff();
}
@AfterClass
public void tearDown() throws Exception {
driver.quit();
}
}