Login through Data Driven Testing in selenium

UserLogin.java

package UserStory;

import org.openqa.selenium.By;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;


import com.aventstack.extentreports.ExtentReports;


import BasePackage.Startup;

import UserStory_webelements.UserLoginwbElements;

import Utility.ExcelFileReader;

import io.github.bonigarcia.wdm.WebDriverManager;


public class UserLogin extends Startup implements UserLoginwbElements {

public static ExtentReports report;

public static ExcelFileReader data1 = new ExcelFileReader(

"C:\\Users\\TechExtensor 21\\Downloads\\DisBox\\src\\main\\resources\\Excelsheet\\MemberLogin.xlsx");


@BeforeTest

public void openDriver() throws InterruptedException {

WebDriverManager.chromedriver().setup();

setBrowsers("Chrome");

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

report = new ExtentReports();

Launchchannel();

Thread.sleep(7000);

}


@Test(dataProvider = "getdataXls")


// @Test

public static void login(String email, String pass) throws InterruptedException {

driver.findElement(emailid).sendKeys(email);

driver.findElement(password).sendKeys(pass);

driver.findElement(Signin).click();

Thread.sleep(4000);

logout();

}


public static void logout() throws InterruptedException {

driver.findElement(avatar).click();

Thread.sleep(3000);

driver.findElement(Logout).click();

}


@DataProvider

public static Object[][] getdataXls() {

return getTestData(data1, "Sheet1");

}


public static Object[][] getTestData(ExcelFileReader data, String sheetname) {

int rows = data.totalRow(sheetname);

int cols = data.totalColumn(sheetname);

System.out.println(rows + cols);

Object[][] myData = new Object[rows - 1][cols];

for (int row = 1; row < rows; row++) {

for (int col = 0; col < cols; col++) {

myData[row - 1][col] = data.getData(sheetname, row, col);

}

}

return myData;

}

}

Interface - UserLoginwbElements.java


package UserStory_webelements;

import org.openqa.selenium.By;

public interface UserLoginwbElements {
By Signin = By.xpath("//button[text()=\" Sign in \"]");
By emailid = By.xpath("//input[@name=\"username\"]");
By password = By.xpath("//input[@name=\"password\"]");
By avatar = By.xpath("/html/body/app-root/app-admin/app-nav-bar/div/app-nav-right/ul/li/div[2]/a/app-avtar-photo");
By Logout = By.xpath("//img[@alt=\"Logout\"]");
}



Startup.java

package BasePackage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.io.FileHandler;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;

import Utility.ExcelFileReader;

public class Startup {

public static WebDriver driver;
public static ExcelFileReader suite;
public static ExcelFileReader DiscountExcel;
public static ExcelFileReader tdata;
public static Properties sitedata;
public static ExtentHtmlReporter reporter = new ExtentHtmlReporter(
System.getProperty("user.dir") + "/src/main/resources/Reports/Reports.html");
public static ExtentReports reports = new ExtentReports();
public static ExtentTest test;

public static void setBrowsers(String Browsername) {
reports.attachReporter(reporter);
ExtentTest Test = reports.createTest("Browser History");
if (Browsername.equalsIgnoreCase("chrome")) {
driver = new ChromeDriver();
Test.log(Status.INFO, "Chrome Browser Loaded Successfully");
driver.manage().window().maximize();
}
if (Browsername.equalsIgnoreCase("Firefox")) {
driver = new FirefoxDriver();
Test.log(Status.INFO, "FireFox Browser Loaded Successfully");
}
if (Browsername.equalsIgnoreCase("Edge")) {
driver = new EdgeDriver();
Test.log(Status.INFO, "Edge Browser Loaded Successfully");
}
reports.flush();
}

public void Launchchannel() {
driver.get("http://techext-001-site22.itempurl.com");
}
public static void exlsheetConnection() {
suite = new ExcelFileReader(
System.getProperty("user.dir") + "/src/main/resources/Excelsheet/DiscussionBoxAutomation.xlsx");
tdata = new ExcelFileReader(System.getProperty("user.dir") + "/src/main/resources/Excelsheet/DBFrontEnd.xlsx");
DiscountExcel = new ExcelFileReader(
System.getProperty("user.dir") + "/src/main/resources/Excelsheet/DiscountDetail.xlsx");

}

public static void epresent() throws IOException {
sitedata = new Properties();
FileInputStream fi = new FileInputStream(
System.getProperty("user.dir") + "/src/main/resources/Properties/sitedata.properties");
sitedata.load(fi);

}

public static String getScreenShot(String imageName, WebDriver driver) {
TakesScreenshot ts = (TakesScreenshot) driver;
File scrFile = ts.getScreenshotAs(OutputType.FILE);
String path = System.getProperty("user.dir") + "/src/main/resources/screenshots/" + imageName
+ System.currentTimeMillis() + ".png";
// System.out.println(path);
File destination = new File(path);
try {
FileHandler.copy(scrFile, destination);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return path;
}

}


POM.XML

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>DbsSaaS</groupId>
<artifactId>s</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.3.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.1.0</version>
</dependency>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
<version>3.17</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-examples -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-examples</artifactId>
<version>3.17</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>4.0.9</version>
</dependency>
</dependencies>

</project>



Comments

Popular posts from this blog

For Get Dynamic data & USe it into Postman Body parameters

What is data driven testing?

API Automation Testing Script to check Response

How to handle frame window in selenium?

How to work with mouse hover menu in selenium?

How does Selenium Webdriver handle SSL certificate in Chrome?