0% found this document useful (0 votes)
26 views

Code For Lesson 6B

The document describes code for automating login tests on an elearning site using Selenium and TestNG. The code uses data-driven testing with login credentials stored in an Excel file and read using a utility class. The test logs in with different credentials, verifies the login succeeded, and quits the browser after each test run.

Uploaded by

duyduy1xx
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Code For Lesson 6B

The document describes code for automating login tests on an elearning site using Selenium and TestNG. The code uses data-driven testing with login credentials stored in an Excel file and read using a utility class. The test logs in with different credentials, verifies the login succeeded, and quits the browser after each test run.

Uploaded by

duyduy1xx
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Code for Lesson 6B

//DDTExcel.java
package New;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DDTExcel {

// TODO Auto-generated method stub

WebDriver driver;

protected static String result;

@Test(dataProvider="testdata")
public void DemoProject(String username, String password)
{

try {
System.setProperty("webdriver.chrome.driver", "D:\\Install
selenium\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();

driver.get("http://demo.guru99.com/test/newtours/");

driver.findElement(By.name("userName")).sendKeys(username);
driver.findElement(By.name("password")).sendKeys(password);

driver.findElement(By.name("submit")).click();

Thread.sleep(5000);

Assert.assertTrue(driver.getTitle().matches("Login: Mercury
Tours"), "Invalid credentials");

System.out.println("Login successful");

} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@AfterMethod

NGUYỄN CÔNG DANH, 03/11/2021 1


void ProgramTermination()
{
driver.quit();
}
@DataProvider(name="testdata")
public Object[][] TestDataFeed()
{
ReadExcelFile config = new
ReadExcelFile("D:\\TestProjects\\newtours\\LoginCredentials.xlsx");

int rows = config.getRowCount(0);

Object[][] credentials = new Object[rows][2];

for(int i=0; i<rows; i++)


{
credentials[i][0] = config.getData(0, i, 0);
credentials[i][1] = config.getData(0, i, 1);
}

return credentials;
}
}

NGUYỄN CÔNG DANH, 03/11/2021 2


//ReadExcelFile.java
package New;

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

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcelFile {

XSSFWorkbook wb;
XSSFSheet sheet;

public ReadExcelFile(String excelPath)


{
try {
File src = new File(excelPath);
FileInputStream fis = new FileInputStream(src);
wb = new XSSFWorkbook(fis);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public String getData(int sheetnumber, int row, int column)


{
sheet = wb.getSheetAt(sheetnumber);
String data = sheet.getRow(row).getCell(column).getStringCellValue();
return data;

public int getRowCount(int sheetIndex)


{
int row = wb.getSheetAt(sheetIndex).getLastRowNum();
row = row +1;
return row;
}
}

NGUYỄN CÔNG DANH, 03/11/2021 3


NGUYỄN CÔNG DANH, 03/11/2021 4
//Exercise: Login into Elearning site
//DDTExcel.java
package New;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DDTExcel {

// TODO Auto-generated method stub

WebDriver driver;

protected static String result;

@Test(dataProvider="xy")
public void DemoProject(String username, String password)
{

try {
/*
System.setProperty("webdriver.gecko.driver",
"D:\\DATA_D\\DBCLvaKTPM HK1 20222023\\install selenium\\
geckodriver-v0.31.0-win64\\geckodriver.exe");

WebDriver driver= new FirefoxDriver();


*/

System.setProperty("webdriver.chrome.driver", "D:\\DATA_D\\DBCLvaKTPM HK1 20222023\\


install selenium\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();

//driver.get("http://demo.guru99.com/test/newtours/");
driver.get("https://elearning.ctu.edu.vn/login/index.php");

driver.findElement(By.name("username")).sendKeys(username);
driver.findElement(By.name("password")).sendKeys(password);

//driver.findElement(By.name("submit")).click();
driver.findElement(By.id("loginbtn")).click();

Thread.sleep(3000);

//Assert.assertTrue(driver.getTitle().matches("Login: Mercury Tours"), "Invalid


credentials");

//String str =
driver.findElement(By.xpath("//*[@id='frontpage-category-names']/h2")).getText();
//String str = driver.findElement(By.xpath("//h2[contains(text(),'Course
categories')]")).getText();
//Assert.assertTrue(str.contains("Course categories"),"Invalid credentials");

//Assert.assertEquals(str, "Course categories");

NGUYỄN CÔNG DANH, 03/11/2021 5


String str = driver.findElement(By.xpath("//*[@id='instance-41-header']")).getText();
Assert.assertTrue(str.contains("Navigation"),"Invalid credentials");

System.out.println("Login successful");

} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@AfterMethod
void ProgramTermination()
{
driver.quit();
}
@DataProvider(name="xy")
public Object[][] TestDataFeed()
{
ReadExcelFile config = new ReadExcelFile("D:\\DATA_D\\DBCLvaKTPM HK1 20222023\\
TestProjects\\Elearning\\LoginCredentials.xlsx");

int rows = config.getRowCount(0);

Object[][] credentials = new Object[rows][2];

for(int i=0; i<rows; i++)


{
credentials[i][0] = config.getData(0, i, 0);
credentials[i][1] = config.getData(0, i, 1);
}

return credentials;
}
}

NGUYỄN CÔNG DANH, 03/11/2021 6

You might also like