Reputation: 1
I have written script for parameterization for login in selenium web driver for valid and invalid entries. script is running fine for valid inputs but stuck for invalid inputs now i want to handle that error for invalid entries and want to resume my script for next input. so how can i do it. please help me.
check for below code
public class Sample {
public static void main(String[] args) throws InterruptedException, BiffException, IOException {
//Login Page
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
//Login parameterization using excel sheet (Jxl.jar)
Workbook wBook = Workbook.getWorkbook(new File("F:\\Selenium\\Parameterize\\login.xls"));
int i = 1;
//Get sheet
Sheet sheet = wBook.getSheet(0);
for(i = 1; i<sheet.getRows(); i++){
driver.get("https://www.Google.com");
//user name
driver.findElement(By.xpath("//*[@id='j_username']")).sendKeys(sheet.getCell(0, i).getContents());
//password
driver.findElement(By.xpath("//*[@id='j_password']")).sendKeys(sheet.getCell(1, i).getContents());
//submit
driver.findElement(By.xpath("//*[@id='signInForm']/div/div/div[7]/div[2]/button")).click();
Thread.sleep(5);
//TO move mouse to display Sign Out drop down window
Actions builder = new Actions(driver);
WebElement getElement = driver.findElement(By.xpath("//*[@id='welcomeSpanUserName']"));
builder.moveToElement(getElement).build().perform();
Thread.sleep(5);
//Click on Sign Out button
driver.findElement(By.xpath("//*[@id='wrapper']/div[2]/ul[2]/li[2]/ul/li[2]/span/a")).click();
}
}
}
Upvotes: 0
Views: 89
Reputation: 5819
Try using try..catch block within the loop and if there is an exception it will proceed with next iteration.
public class Sample {
public static void main(String[] args) throws InterruptedException, BiffException, IOException {
//Login Page
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
//Login parameterization using excel sheet (Jxl.jar)
Workbook wBook = Workbook.getWorkbook(new File("F:\\Selenium\\Parameterize\\login.xls"));
int i = 1;
//Get sheet
Sheet sheet = wBook.getSheet(0);
for(i = 1; i<sheet.getRows(); i++){
try{
driver.get("https://115.112.36.94:7443/");
//user name
driver.findElement(By.xpath("//*[@id='j_username']")).sendKeys(sheet.getCell(0, i).getContents());
//password
driver.findElement(By.xpath("//*[@id='j_password']")).sendKeys(sheet.getCell(1, i).getContents());
//submit
driver.findElement(By.xpath("//*[@id='signInForm']/div/div/div[7]/div[2]/button")).click();
Thread.sleep(5);
//TO move mouse to display Sign Out drop down window
Actions builder = new Actions(driver);
WebElement getElement = driver.findElement(By.xpath("//*[@id='welcomeSpanUserName']"));
builder.moveToElement(getElement).build().perform();
Thread.sleep(5);
//Click on Sign Out button
driver.findElement(By.xpath("//*[@id='wrapper']/div[2]/ul[2]/li[2]/ul/li[2]/span/a")).click();
}catch(Exception e){
//handle exception if you need
}
}
}
}
Upvotes: 1