Step 1:- Create Maven Project
Step 2:- Go to src/main/java and create below classes
=========================================================================
package com.liveguru99.utils;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class GetData
{
public static String fromExcel(String fileName, String
sName, int rIndex, int cIndex)
{
String data= null;
try
{
//File f = new File("./data/"+fileName+".xlsx");
File f = new File("D:\\AmazonTestCase.xlsx");
FileInputStream fis = new FileInputStream(f);
Workbook wb = WorkbookFactory.create(fis);
Sheet st = wb.getSheet(sName);
Row r = st.getRow(rIndex);
Cell c = r.getCell(cIndex);
data = c.toString();
}
catch(Exception e)
{
}
return data;
}
public static String fromProperties(String fileName, String key)
{
String value = null;
try
{
//File f = new File("./config/"+fileName+".properties");
File f=new File("F:\\MyWorkSpace\\MultpleApp\\config\\config.properties");
FileInputStream fis = new FileInputStream(f);
Properties prop = new Properties();
prop.load(fis);
value = prop.getProperty(key);
}
catch(Exception e)
{
e.printStackTrace();
}
return value;
}
}
=========================================================================
package com.liveguru99.utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
public class Handlers
{
public static void acceptAlert(WebDriver driver)
{
Alert alt = driver.switchTo().alert();
alt.accept();
}
public static void dismissAlert(WebDriver driver)
{
Alert alt = driver.switchTo().alert();
alt.dismiss();
}
public static String getAlertText(WebDriver driver)
{
Alert alt = driver.switchTo().alert();
String text = alt.getText();
return text;
}
public static void enterTextToAlert(WebDriver driver, String data)
{
Alert alt = driver.switchTo().alert();
alt.sendKeys(data);
}
public static void selectDDLByIndex(WebElement element, int index)
{
Select sct = new Select(element);
sct.selectByIndex(index);
}
public static void selectDDLByText(WebElement element, String text)
{
Select sct = new Select(element);
sct.selectByVisibleText(text);
}
public static void selectDDLByValue(WebElement element, String value)
{
Select sct = new Select(element);
sct.selectByValue(value);
}
public static boolean isDDLMultiple(WebElement element)
{
Select sct = new Select(element);
boolean status = sct.isMultiple();
return status;
}
public static void ActionsmoveToElement(WebElement ele,WebDriver driver){
Actions act=new Actions(driver);
act.moveToElement(ele).perform();
}
public static void wait(int i){
try {
Thread.sleep(i);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
=========================================================================
package com.liveguru99.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class SetData
{
public static void toExcel(String fileName, String sName,
int rIndex, int cIndex, String data)
{
try
{
File f = new File("./data/"+fileName+".xlsx");
FileInputStream fis = new FileInputStream(f);
Workbook wb = WorkbookFactory.create(fis);
Sheet st = wb.getSheet(sName);
Row r = st.getRow(rIndex);
Cell c = r.createCell(cIndex);
c.setCellValue(data);
FileOutputStream fos = new FileOutputStream(f);
wb.write(fos);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void toProperties(String fileName,
String key, String value, String commitMsg)
{
try
{
File f = new File("./data/"+fileName+".properties");
FileInputStream fis = new FileInputStream(f);
Properties prop = new Properties();
prop.load(fis);
prop.setProperty(key, value);
FileOutputStream fos = new FileOutputStream(f);
prop.store(fos, commitMsg);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
=========================================================================
Step 3:- Create CreateDriver Class
=========================================================================
package com.liveguru99project.utils;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.liveguru99.utils.GetData;
public class CreateDriver
{
public static WebDriver getDriver()
{
WebDriver driver = null;
String browserName = GetData.fromProperties("config","browser");
String url = GetData.fromProperties("config", "url");
if(browserName.equalsIgnoreCase("ff"))
{
System.setProperty("webdriver.gecko.driver",
"D:\\study meterial\\AutomationData_new_NEW\\FrameWork\\OSCW-10\\sonar-cubes\\browser_server\\geckodriver.exe");
driver = new FirefoxDriver();
}
else if(browserName.equalsIgnoreCase("gc"))
{
System.setProperty("webdriver.chrome.driver","C:\\Users\\user\\Desktop\\Driver\\chromedriver.exe");
driver=new ChromeDriver();
}
else
{
System.out.println("Invalid Browser name");
}
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(url);
return driver;
}
}
========================================================================
Step 5:- Create Properties File .
Step 6:- Create Page Object Model
=========================================================================
package com.pageobjectamazon;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class Loginpage {
WebDriver driver;
public Loginpage(WebDriver driver){
this.driver=driver;
}
@FindBy(xpath="//span[contains(text(),'Hello, Sign in')]")
WebElement helloSignin;
//@FindBy(xpath="//div[@id='nav-flyout-ya-signin']/a/span")
@FindBy(xpath="//*[@id='nav-flyout-ya-signin']/a/span")
////*[@id="nav-flyout-ya-signin"]/a/span
WebElement signIn;
@FindBy(id="ap_email")
WebElement emailormobileNumField;
@FindBy(id="continue")
WebElement continueBtn;
@FindBy(id="ap_password")
WebElement passwordField;
@FindBy(xpath="//*[@id='signInSubmit']")
WebElement logInButn;
@FindBy(xpath="//span[contains(text(),'Hello, Mohit')]")
WebElement hellousr;
public WebElement helloSignIN(){
WebElement element= helloSignin;
System.out.println(element);
return element;
}
public void clickOnSignIn(){
try{
signIn.click();
}
catch(Exception e){
e.printStackTrace();
}
}
public void enterEmailOrMobileNum(String emailMobileNumber){
try{
emailormobileNumField.sendKeys(emailMobileNumber);
}
catch(Exception e){
e.printStackTrace();
}
}
public void clickOnContine(){
try{
continueBtn.click();
}
catch(Exception e){
e.printStackTrace();
}
}
public void enterPassword(String password){
try{
passwordField.sendKeys(password);
}
catch(Exception e){
e.printStackTrace();
}
}
public void clickOnLogin(){
try{
logInButn.click();
}
catch(Exception e){
e.printStackTrace();
}
}
}
=======================================================================
Step 6:- Go to src/test java and Create Test Runner Class .
========================================================================
package com.amazontest;
import org.apache.commons.collections4.Get;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.liveguru99.utils.GetData;
import com.liveguru99.utils.Handlers;
import com.liveguru99project.utils.CreateDriver;
import com.pageobjectamazon.Loginpage;
public class LoginAmazonTest {
public WebDriver driver;
public Loginpage newloginPage;
public Handlers handler;
@BeforeTest
public void setUp(){
driver=CreateDriver.getDriver();
newloginPage=new Loginpage(driver);
}
@Test
public void AM_01(){
Loginpage page=PageFactory.initElements(driver, Loginpage.class);
WebElement ele=page.helloSignIN();
handler.ActionsmoveToElement(ele, driver);
page.clickOnSignIn();
String userName= GetData.fromExcel("AmazonTestCase.xlsx", "Sheet1", 12, 4);
System.out.println("userName --->" + userName);
page.enterEmailOrMobileNum(userName);
page.clickOnContine();
String password= GetData.fromExcel("AmazonTestCase.xlsx", "Sheet1", 14, 4);
page.enterPassword(password);
System.out.println("password --->" + password);
page.clickOnLogin();
}
}
=========================================================================
Output:-
No comments:
Post a Comment