public class AustriaRegro {
public String Result;
WebDriver driver;
WebDriverWait wait;
Sheet s;
WritableSheet ws;
WritableWorkbook wc;
Workbook w;
@Test
public void TestSetup() throws IOException, BiffException, RowsExceededException, WriteException{
System.setProperty("webdriver.chrome.driver","C:\\Users\\yirsh\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe");
driver=new ChromeDriver();
FileInputStream fi = new FileInputStream("C:\\Users\\yirsh\\Desktop\\UAT WEBSHOP.xls");
w = Workbook.getWorkbook(fi);
s = w.getSheet("Regro");
FileOutputStream fo = new FileOutputStream("C:\\Users\\yirsh\\Desktop\\UATWEBSHOPResult.xls");
wc =Workbook.createWorkbook(fo);
ws = wc.createSheet("Query_data", 0);
Sheet sheets = w.getSheet("Query_data");
String inputdata[] [] = new String[s.getRows()][s.getColumns()];
for (int i=0;i<s.getRows();i++)
{
for(int k=0;k< s.getColumns();k++)
{
inputdata[i][k] = s.getCell(k,i).getContents();
Label l = new Label(k,i , inputdata[i][k]);
Label la = new Label(4,0,"Results");
ws.addCell(l);
ws.addCell(la);
}
}
for(int row=1; row <= s.getRows() ; row++){
driver.manage().deleteAllCookies();
wait = new WebDriverWait(driver,30);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://path/");
driver.manage().window().maximize();
String username = s.getCell(2,row).getContents();
System.out.println("***********************************");
System.out.println("Username: "+username);
driver.findElement(By.id("j_username_leftAside")).sendKeys(username);
String password= s.getCell(3,row).getContents();
System.out.println("Password: "+password);
driver.findElement(By.id("j_password_leftAside")).sendKeys(password);
driver.findElement(By.xpath("/html[@class=' js opacity generatedcontent pointerevents']/body[@class='page-homepage pageType-ContentPage template-pages-layout-RexelHomePageLayout pageLabel-homepage language-de ']/div[@id='page']/div[@id='content']/div[@id='content']/div[@class='content-top-inner']/div[@id='content-inner']/div[@class='mid-wrapper'][1]/div[@class='yCmsContentSlot']/div[@class='login clear']/form[@id='loginForm']/div[@class='left sign-in']/button[@class='Sign-in rx-btn mb0']")).click();
try{
if((driver.findElement(By.xpath(".//*[@id='globalMessages']/div"))).isDisplayed()){
System.out.println("Login Failed");
Result="Failed";
String Error=driver.findElement(By.xpath(".//*[@id='globalMessages']/div")).getText();
System.out.println("The Error mesaage is :"+Error);
System.out.println("***********************************************************************************************************");
}
}
catch (Exception e){
System.out.println("Login Sucessfull");
Result="Pass";
System.out.println("***********************************");
driver.findElement(By.xpath(".//*[@id='content-inner']/div[1]/div/div[2]/div[3]/div/div/ul/li[9]/a")).click();
}
Label lb = new Label(4,1,Result);
ws.addCell(lb);
}
driver.close();
}
}
我正在编写一个代码来从excel表中获取数据并对其运行selenium测试并将输出写入另一个excel文件中。结果Excel已创建,但未插入数据。需要更正代码,以便可以插入结果。只有将结果保存在Excel中才会保持正常工作正常。
答案 0 :(得分:1)
您需要写入文件。我相信你可以通过致电WritableWorkbook.write()
来做到这一点。记得以后关闭文件。
...
...
Label lb = new Label(4,1,Result);
ws.addCell(lb);
}
wc.write();
wc.close();
w.close();
driver.close();
请参阅documentation here。 write函数可以抛出IOException,所以你必须在某个时刻处理它 - 我看到你将该异常传递到堆栈中,这很好。