0% found this document useful (0 votes)
2K views5 pages

5.HandsOn Page Factory Method

Selenium webDriver handson solutions - TCS FRESCOPLAY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views5 pages

5.HandsOn Page Factory Method

Selenium webDriver handson solutions - TCS FRESCOPLAY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

5.

Hands-On: Page Factory Method


Welcome to Page Factory Method

File Name - verfiyLogin_PageFactory.java

package TestCases;

import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import Pages.PageFactoryLogin;
import utility.BrowserFactory;

public class verfiyLogin_PageFactory {

WebDriver driver;

@Test
public void checkValidUser(){

WebDriver driver = BrowserFactory.startBrowser("PhantomJS",


"https://en.wikipedia.org/wiki/Main_Page");
PageFactoryLogin login_page = PageFactory.initElements(driver,
PageFactoryLogin.class);

// Write your username and password in below line

login_page.Login("Abarna6898","abcdefg");
System.out.println("Page Title : " + driver.getTitle());
driver.quit();
System.out.println("Browser Quit is successful");
}

File Name - BrowserFactory.java

package utility;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

public class BrowserFactory {

static WebDriver driver;

public static WebDriver startBrowser(String browserName, String url ){

if (browserName.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver",
"/root/selenium/chromedriver");
driver=new ChromeDriver();
}

else if (browserName.equalsIgnoreCase("HtmlUnitDriver")){
driver = new HtmlUnitDriver(true);
}

else if (browserName.equalsIgnoreCase("PhantomJS"))
{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
String [] phantomJsArgs = {"--ignore-ssl-errors=yes"};
caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,
phantomJsArgs);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECU
TABLE_PATH_PROPERTY,
"/projects/challenge/phantomjs-2.1.1-linux-x86_64/bin/phantomjs");
driver = new PhantomJSDriver(caps);
System.out.println("PhantomJS Headless Driver launched");
}

// Write your script here

driver.manage().window().maximize();
driver.get(url);

return driver;
}
}

File Name - PageFactoryLogin.java


package Pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

public class PageFactoryLogin {

WebDriver driver;

public PageFactoryLogin(WebDriver localdriver){


this.driver=localdriver;
}

// Write your script here

@FindBy(xpath = "//*[@id='pt-login']/a")WebElement loginTab;


@FindBy(id = "wpName1")WebElement username;
@FindBy(how = How.ID,using = "wpPassword1")WebElement password;
@FindBy(how = How.XPATH,using =
"//*[@id='wpLoginAttempt']")WebElement logIn;

public void Login(String uid, String pass){


loginTab.click();
username.sendKeys(uid);
password.sendKeys(pass);
logIn.click();
}
}

You might also like