Advertisement
NexGenration

Selenium LEX 4

Dec 4th, 2021
4,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. package com.test;
  2. import java.io.FileInputStream;
  3. import java.util.concurrent.TimeUnit;
  4. import org.apache.poi.xssf.usermodel.XSSFSheet;
  5. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  6. import org.junit.After;
  7. import org.junit.Before;
  8. import org.junit.Test;
  9. import org.openqa.selenium.By;
  10. import org.openqa.selenium.WebDriver;
  11. import org.openqa.selenium.firefox.FirefoxDriver;
  12. public class Demo010_ExcelReading {
  13.     WebDriver driver;
  14.     String url = "http://localhost:8080/PackAndGo_v2/index.html";
  15.  
  16.     @Before
  17.     public void setUp() {
  18.         //Set the key/value property according to the browser you are using.
  19.         System.setProperty("webdriver.gecko.driver",driverPath+"geckodriver.exe");
  20.                  
  21.         //Open browser instance
  22.         driver = new FirefoxDriver();
  23.                        
  24.         //Open the AUT
  25.         driver.get(url);
  26.        
  27.         //Declare an implicit wait which is bounded to WebDriver instance
  28.         driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
  29.     }
  30.     @SuppressWarnings("resource")
  31.     @Test
  32.     public void test() throws Exception {
  33.         //Path from where the excel file has to be read
  34.         String filePath = System.getProperty("user.dir") + "\\Cred.xlsx";
  35.          
  36.                 //File input stream which needs the input as the file location
  37.                 FileInputStream fileStream = new FileInputStream(filePath);
  38.                
  39.                 //Workbook reference of the excel file
  40.                 XSSFWorkbook workbook = new XSSFWorkbook(fileStream);
  41.                
  42.                 //Sheet which needs to be accessed from within the workbook
  43.                 XSSFSheet sheet = workbook.getSheet("Sheet1");
  44.          
  45.                 //Count the number of rows
  46.                 int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
  47.          
  48.                 //Iterate the Username/Password/
  49.                 for (int i = 1; i <= rowCount; i++) {
  50.                     //Pass the row number and the cell number from where the value has to be fetched
  51.                    
  52.                     driver.findElement(By.xpath("//*[@id=\"myNavbar\"]/ul/li[4]/a")).click();      
  53.                    
  54.                     Thread.sleep(2000);
  55.                    
  56.                     driver.findElement(By.id("usernameLogin")).sendKeys(sheet.getRow(i).getCell(0).getStringCellValue());
  57.                    
  58.                    
  59.                     driver.findElement(By.id("passwordLogin")).sendKeys(sheet.getRow(i).getCell(1).getStringCellValue());
  60.                    
  61.                    
  62.                     driver.findElement(By.id("login")).click();
  63.                    
  64.                     String message = driver.findElement(By.xpath("/html/body/div[1]/div/div[1]/div/div[1]")).getText();
  65.                     System.out.println(message);
  66.          
  67.                     driver.findElement(By.linkText("LogOut")).click();
  68.                 }
  69.     }
  70.    
  71.     @After
  72.     public void tearDown() throws Exception {
  73.         driver.close();
  74.     }
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement