[英]How to read test data from excel sheet and provide into webpage and mark test case as pass or fail accordigly in selenium web script using POI
[英]How do I read data from excel sheet and use data as an input for webpage using selenium ? I am able to read only 1 row at a time. Code displayed:
我剛剛開始學習 selenium,我無法自動化代碼,每次只能從 excel 讀取一個。我需要從 excel 中讀取代碼。 = 1;i<=6;i++)。"
如何讓它從下面的代碼中自動讀取?
public static void main(String[] args) throws IOException, InterruptedException {
System.setProperty("driver location");
WebDriver driver = new FirefoxDriver();
driver.get("link");
FileInputStream file = new FileInputStream("xcel file location");
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet= workbook.getSheet("SO Reg");
int noOfRows = sheet.getLastRowNum(); // returns the row count
System.out.println("No. of Records in the Excel Sheet:" + noOfRows);
int cols=sheet.getRow(1).getLastCellNum();
System.out.println("No. of Records in the Excel Sheet:" + cols);
for (int i= 1; i<=6; i++)
{
String SO_Name = row.getCell(0).getStringCellValue();
String Contact_Person = row.getCell(1).getStringCellValue();
String Address_1 = row.getCell(2).getStringCellValue();
String Address_2 = row.getCell(3).getStringCellValue();
String City = row.getCell(4).getStringCellValue();
String State = row.getCell(5).getStringCellValue();
String ZipCode = row.getCell(6).getStringCellValue();
String Phone_Number = row.getCell(8).getStringCellValue();
String Username = row.getCell(9).getStringCellValue();
String Email = row.getCell(10).getStringCellValue();
String Re_Type_Email = row.getCell(11).getStringCellValue();
//Registration Process
driver.findElement(By.cssSelector("p.text-white:nth-child(4) > a:nth-child(1)")).click(); //create an account
Thread.sleep(5000);
//Enter Data information
driver.findElement(By.id("SOName")).sendKeys(SO_Name);
driver.findElement(By.xpath("//*[@id=\"ContactPerson\"]")).sendKeys(Contact_Person);
driver.findElement(By.xpath("//*[@id=\"AddressLine1\"]")).sendKeys(Address_1);
driver.findElement(By.xpath("//*[@id=\"AddressLine2\"]")).sendKeys(Address_2);
driver.findElement(By.id("City")).sendKeys(City);
driver.findElement(By.id("State")).sendKeys(State);
driver.findElement(By.id("ZipCode")).sendKeys(ZipCode);
driver.findElement(By.id("Phone")).sendKeys(Phone_Number);
driver.findElement(By.xpath("//*[@id=\"UserName\"]")).sendKeys(Username);
driver.findElement(By.xpath("//*[@id=\"Email\"]")).sendKeys(Email);
driver.findElement(By.xpath("//*[@id=\"RandText\"]")).sendKeys(Re_Type_Email);
driver.findElement(By.id("ConfirmBox")).click();
driver.findElement(By.xpath("/html/body/app-root/app-soregistration/div[2]/div/div/div/div/form[2]/div/div[12]/div/button[1]")).click();
driver.findElement(By.cssSelector(".btn-green-text-black")).click(); //finish button
driver.findElement(By.cssSelector("p.text-white:nth-child(4) > a:nth-child(1)")).click(); //create an account
Thread.sleep(5000);
}
}
}
}
}
您只想處理 Excel 中新添加的行嗎? 如果是這樣,您還應該保存您的最后一次住宿。 首先,您可以簡單地將其保持在無限循環中。 喜歡
while(true){...}
. 您還可以通過將您從 Excel 中讀取的最后一行保存在靜態變量中來開始循環。 例如:
for (int i= previusLastSavedRowNum; i<=getLastRowNum; i++) {...}
如果沒有新記錄,可以在 WHILE 循環中等待一段時間。
當然,為了更好的解決方案,您可以創建一個 SpringBoot 項目,並在 Excel 中設置一個監聽變化的結構。 當 Excel 檢測到更改時,您可以使用觸發器調用 Selenium 代碼。
更好的處理方法是將 excel 文件作為 CSV 打開。 您可以使用以下命令將所有數據讀入一個字符串: String excelToString = new String(Files.readAllBytes(Paths.get(path_to_file))); 如果您想將其保留為表格,您可以將此字符串解析為 String [] [] 表格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.