I am getting StaleElementReferenceException in the fourth Test. I tried using the explicit wait to ignore the exception and wait for the element to be clickable which was suggested in a similar question in this forum ref: How to avoid "StaleElementReferenceException" in Selenium?.
public class AdminInterface {
@Test(priority=1)
public void adminToUserInterface() throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.gcrit.com/build3/admin");
driver.findElement(By.xpath("//a[text()='Online Catalog']")).click();
String str = "welcome";
String str1 = (driver.findElement(By.xpath("//div[@id='bodyContent']/div[1]/div[1]")).getText()).toLowerCase();
if(str1.contains(str))
System.out.println("Redirecting to user interface from admin interface successfull == PASS");
else
System.out.println("Redirecting to user interface from admin interface failed == FAILED");
Thread.sleep(2000);
driver.close();
}
@Test(priority=2)
public void loginTest() throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.gcrit.com/build3/admin");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin@123");
driver.findElement(By.id("tdb1")).click();
String str = "Catalog";
String str1=driver.findElement(By.xpath("//div[@id='adminAppMenu']/h3[1]/a")).getText();
if(str.equals(str1))
System.out.println("Login was successful==PASS");
else
System.out.println("Login was not successful==FAIL");
Thread.sleep(2000);
driver.close();
}
@Test(priority=3,dependsOnMethods={"loginTest"})
public void addManufacturer() throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.gcrit.com/build3/admin");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin@123");
driver.findElement(By.id("tdb1")).click();
driver.findElement(By.xpath("//a[text()='Manufacturers']")).click();
driver.findElement(By.xpath("//span[text()='Insert']")).click();
driver.findElement(By.name("manufacturers_name")).sendKeys("AA1");
driver.findElement(By.id("tdb1")).click();
String str = "AA1";
List<WebElement> tds =driver.findElements(By.tagName("td"));
Iterator <WebElement> itr = tds.iterator();
while(itr.hasNext())
{
if((itr.next().getText()).equals(str))
{
System.out.println("Manufacturer creation was successfull==PASS");
}
}
Thread.sleep(4000);
driver.close();
}
@Test(priority=4,dependsOnMethods={"addManufacturer"})
public void editManufacturer() throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.gcrit.com/build3/admin");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin@123");
driver.findElement(By.id("tdb1")).click();
driver.findElement(By.xpath("//a[text()='Manufacturers']")).click();
String str = "AA1";
List<WebElement> lst = driver.findElements(By.tagName("td"));
Iterator<WebElement> itr = lst.iterator();
while(itr.hasNext())
{
if((itr.next().getText()).equals(str))
{
itr.next().click();
}
}
new WebDriverWait(driver,20).ignoring(StaleElementReferenceException.class).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@id='tdb2']/span[2]")));
driver.findElement(By.xpath("//a[@id='tdb2']/span[2]")).click();
driver.findElement(By.name("manufacturers_name")).sendKeys("AA2");
driver.findElement(By.id("tdb1")).click();
String str1="AA2";
List<WebElement> lst1 = driver.findElements(By.tagName("td"));
Iterator<WebElement> itr1 = lst1.iterator();
while(itr1.hasNext())
{
int i =0;
if((itr1.next().getText()).equals(str1))
{
System.out.println("The manufacturer "+str+"'s name was successully changed to "+str1+"==PASS");
i++;
}
if(i==0)
{
System.out.println("The manufaturer "+str+"'s name could not be changed");
}
}
Thread.sleep(4000);
driver.close();
}
@Test(priority=5,dependsOnMethods={"editManufacturer"})
public void deleteManufaturer() throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.gcrit.com/build3/admin");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin@123");
driver.findElement(By.id("tdb1")).click();
driver.findElement(By.xpath("//a[text()='Manufacturers']")).click();
String str = "AA2";
List<WebElement> lst = driver.findElements(By.tagName("td"));
Iterator<WebElement> itr = lst.iterator();
while(itr.hasNext())
{
if((itr.next().getText()).equals(str))
{
itr.next().click();
}
}
new WebDriverWait(driver,20).ignoring(StaleElementReferenceException.class).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Delete']")));
driver.findElement(By.xpath("//span[text()='Delete']")).click();
driver.findElement(By.id("tdb1")).click();
List<WebElement> lst1 = driver.findElements(By.tagName("td"));
Iterator<WebElement> itr1= lst1.iterator();
int i=0;
while(itr1.hasNext())
{
if((itr1.next().getText()).equals(str))
{
System.out.println("Deleting the manufaturer "+str+" was unsuccessful == FAIL");
i=1;
}
}
if(i==0)
{
System.out.println("Deleting the manufacturer "+str+" was successful == PASS");
}
}
}