我正在尝试理解和练习阅读和Excel文件并将登录凭据发送到网页。在测试excel文件中,我有2个用户名和2个密码。第1集正常工作(意味着发送到网页字段),但在第2集中我遇到以下问题:
线程中的异常" main" org.openqa.selenium.NoSuchWindowException:找不到窗口。浏览器窗口可能已关闭。 命令持续时间或超时:221毫秒
以下是代码:
public class EX1 {
public static WebDriver driver;
public int cellNo;
public WebDriver utility(String browser, String url){
if(browser.equals("ff")){
driver= new FirefoxDriver();
}
driver.get(url);
return driver;
}
public ArrayList<String> readExcel(int cellNo) throws IOException{
FileInputStream fis = new FileInputStream("C:\\dev\\Petco.xls");
HSSFWorkbook workbook = new HSSFWorkbook(fis);
HSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator= sheet.iterator();
rowIterator.next();// Escapes the Header
ArrayList<String> list= new ArrayList<String>(); //After reading the data I will add them to String list
while(rowIterator.hasNext()){
//list.add(rowIterator.next().getCell(0).getStringCellValue());//will just print 1st username
//list.add(rowIterator.next().getCell(1).getStringCellValue()); // will just print 1st password
//The above code with hardcoded 0,1- I can't call them in below getPage function. I will
//make a parameter for this method which I will pass, cellNo is a parameter instead of 0,1
list.add(rowIterator.next().getCell(cellNo).getStringCellValue());//will just print 1st username
}
return list;
}
public void loginPage(String uname,String psd){
WebElement username=driver.findElement(By.xpath("//input[@id='WC_AccountDisplay_FormInput_logonId_In_Logon_1']"));
WebElement password=driver.findElement(By.xpath("//input[@id='WC_AccountDisplay_FormInput_logonPassword_In_Logon_1']"));
WebElement login_btn=driver.findElement(By.xpath("//button[contains(@id,'WC_AccountDisplay_links_2')]"));
username.sendKeys(uname);
password.sendKeys(psd);
login_btn.click();
}
public void getPage() throws InterruptedException, IOException{
EX1 e= new EX1();
e.utility("ff", "https://www.petco.com/shop/AjaxLogonForm?catalogId=10051&myAccountActivePage=myAccount&myAcctMain=1&langId=-1&storeId=10151");
EX1 e1=PageFactory.initElements(driver, EX1.class);
//Will use the list from above that contains the login information here
ArrayList<String> username=e.readExcel(0); //-- reading the 1st column which has user name
ArrayList<String> password=e.readExcel(1); //-- reading the 1st column which has user name
for(int i=0; i<username.size(); i++){ //-- I want to read excel multiple times which is why the for loop
e1.loginPage(username.get(i), password.get(i));
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.close();
}
}
public static void main(String[] args) throws IOException, InterruptedException {
EX1 e= new EX1();
e.getPage();
//e.readExcel();
}
}
我尝试过Thread.sleep,driver.quit()这些修改没有任何影响。
让我知道我还能尝试什么,谢谢你的时间。
答案 0 :(得分:0)
错误似乎是第一次迭代后浏览器窗口关闭...运行时观察您的测试用例...看到它在第一次迭代后关闭浏览器窗口,即driver.close();
..删除此行并放置在某处之后循环完成,可能会帮助你。例如
for(int i=0; i<username.size(); i++){ //-- I want to read excel multiple times which is why the for loop
e1.loginPage(username.get(i), password.get(i));
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
driver.close();