-1

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");
        }
    }


}
Gaurav Thantry
  • 753
  • 13
  • 30
  • Where exactly do you get the exception? – Guy Aug 29 '17 at 11:37
  • @Guy In the 4th test driver.findElement(By.xpath("//a[@id='tdb2']/span[2]")).click(); – Gaurav Thantry Aug 29 '17 at 16:12
  • 1
    Please provide a [mcve]. Nobody is going to read through that much code. – SiKing Aug 29 '17 at 16:50
  • @SiKing I had to post the whole code because the exception occurs in every other tests on the web page leaving the first two tests. I have already given the part where the exception occurs, in my previous comment. – Gaurav Thantry Aug 29 '17 at 16:56
  • @GauravThantry You didn't *have* to post it all.. you chose to. Clean it down to a MCVE and update your question before it gets closed. – JeffC Aug 29 '17 at 20:57

2 Answers2

1

Instead of -

driver.findElement(By.xpath("//a[@id='tdb2']/span[2]")).click();

Try below code-

element = driver.findElement(By.xpath("//a[@id='tdb2']/span[2]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Kapil
  • 397
  • 4
  • 6
  • Thank you @Kapil your suggestion was very resourceful for this question. The mistake I did was that I forgot to add 'break' after the click statement in the while loop. The test execution was stuck inside the while loop. – Gaurav Thantry Aug 30 '17 at 08:45
0

Guys I found the solution for this question. The jvm was stuck inside the while loop after the click action iterating all the available elements. A 'break' statement has to be used to branch out of the loop when the element is found. Please refer to the code below:

original code: in @Test 4

 while(itr.hasNext())
        {
            if((itr.next().getText()).equals(str))
                    {
                   itr.next().click();
                    }
        }

Updated code:

 while(itr.hasNext())
        {
            if((itr.next().getText()).equals(str))
                    {
                   itr.next().click();
                   break;
                    }
        }

The same goes with the 5th TestScript.

Gaurav Thantry
  • 753
  • 13
  • 30