使用硒进行登录过程测试,并尝试将电子邮件,密码,结果(通过或失败)写入excel工作表。无法检索电子邮件和密码。输出[[ChromeDriver:XP上的chrome(cf34dfe884416c904154a9593f214eea)]->名称:电子邮件]。
已经尝试使用String email2 = email1.toString()和String email2 = driver.findElement(By.name(“ email”))。getText();
对于getText()是Excel工作表空白。 对于toString()excel工作表[[ChromeDriver:XP上的Chrome(cf34dfe884416c904154a9593f214eea)]->名称:电子邮件]。
公共类Classbyclass {
static int initial_row=1 ;
public static void main (String [] args) throws Throwable {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\sha\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
FileInputStream fis = new FileInputStream("C:\\Users\\shahira.azlan\\Desktop\\DataTest1.xlsx");
driver.get("https://www.zgold-qa.razer.com/");
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
Thread.sleep(4000);
driver.findElement(By.xpath("/html/body/div/nav/div/ul/li[3]/a/span")).click();
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheet("login");
Row row =sheet.getRow(0);
Cell cell = row.getCell(0);
for(int i=1; i<=5; i++) {
cell = sheet.getRow(i).getCell(0);
WebElement email1 = driver.findElement(By.name("email"));
//System.out.println(email1);
email1.sendKeys(cell.getStringCellValue());
String email2 = email1.toString();
System.out.println(email1);
//String email2 =driver.findElement(By.name("email")).getText();
cell = sheet.getRow(i).getCell(1);
WebElement password1 = driver.findElement (By.name("password"));
password1.sendKeys(cell.getStringCellValue());
String password2= password1.toString();
//String password2 =driver.findElement(By.name("password")).getText();
driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/form/div[4]")).click();
Thread.sleep(2000);
driver.findElement(By.name("email")).clear();
verifylogin(driver, password2, email2);
}
}
public static void verifylogin(WebDriver driver, String password2, String email2)throws Throwable{
String testcase = "C:\\Users\\sha\\Desktop\\DataTest1.xlsx";
if( driver.findElements( By.className("description") ).size() != 0){
String Result1 = "Pass";
TestResult testresult = new TestResult();
testresult.TR_Result(testcase,password2,email2, Result1);
}
else {
String Result1 = "Fail";
TestResult testresult = new TestResult();
testresult.TR_Result(testcase,password2,email2, Result1);
}
}
public static class TestResult{
void TR_Result(String testcase,String password2, String email2, String Result1) throws IOException{
File file = new File("C:\\Users\\sha\\Desktop\\DataTest1.xlsx");
if(file.exists()){
addTestResult(password2, email2, Result1);
}
else{
createTestResult(testcase);
addTestResult(password2, email2, Result1);
}
}
void createTestResult(String testcase) throws IOException{ //Create test result with headers only
FileOutputStream fos = new FileOutputStream(" C:\\Users\\sha\\Desktop\\DataTest1.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook();
System.out.println(workbook);
XSSFSheet sheet1 = workbook.createSheet("Test Result");
int a = 0;
Row row = sheet1.createRow(a);
Cell result = row.createCell(0);
sheet1.autoSizeColumn(0);
Cell email1 = row.createCell(1);
email1.setCellValue("Email");
sheet1.autoSizeColumn(1);
Cell password1 = row.createCell(2);
password1.setCellValue("Password");
sheet1.autoSizeColumn(2);
Cell result1 = row.createCell(3);
result1.setCellValue("Result");
sheet1.autoSizeColumn(3);
workbook.write(fos);
fos.close();
workbook.close();
}
void addTestResult(String email2, String password2, String Result1) {
try {
int row1 = initial_row++;
if(Result1 == "Pass"){
//driver.findElement(By.id("btn-skip")).click();
System.out.println("valid");
FileInputStream input_pass = new FileInputStream("C:\\Users\\sha\\Desktop\\DataTest1.xlsx");
XSSFWorkbook wb_pass = new XSSFWorkbook(input_pass);
XSSFSheet sheet2 = wb_pass.getSheet("login");
//XSSFRow row_pass = sheet2.getRow(i);
Row row_Pass = sheet2.createRow(row1);
Cell email_pass = row_Pass.createCell(0);
email_pass.setCellValue(email2);
sheet2.autoSizeColumn(0);
Cell pass_pass = row_Pass.createCell(1);
pass_pass.setCellValue(password2);
sheet2.autoSizeColumn(1);
Cell result_pass = row_Pass.createCell(2);
result_pass.setCellValue("PASS");
sheet2.autoSizeColumn(2);
FileOutputStream write_pass = new FileOutputStream("C:\\Users\\sha\\Desktop\\DataTest1.xlsx");
wb_pass.write(write_pass);
write_pass.close();
wb_pass.close();
}
else{
System.out.println("invalid");
FileInputStream input_Fail = new FileInputStream("C:\\Users\\sha\\Desktop\\DataTest1.xlsx");
XSSFWorkbook wb_Fail = new XSSFWorkbook(input_Fail);
XSSFSheet sheet2 = wb_Fail.getSheet("login");
Row row_fail = sheet2.createRow(row1);
Cell email_fail = row_fail.createCell(0);
email_fail.setCellValue(email2);
sheet2.autoSizeColumn(0);
//System.out.println(email2);
Cell pass_fail = row_fail.createCell(1);
pass_fail.setCellValue(password2);
sheet2.autoSizeColumn(1);
Cell result_fail = row_fail.createCell(2);
result_fail.setCellValue("FAIL");
sheet2.autoSizeColumn(2);
FileOutputStream write_Fail = new FileOutputStream("C:\\Users\\sha\\Desktop\\DataTest1.xlsx");
wb_Fail.write(write_Fail);
write_Fail.close();
wb_Fail.close();
}
}
catch(Exception e ){
}
}
}