0

I am working with some software that schedule reports to go out to clients. Sometimes in running my selenium tests something goes wrong and it does not delete the instance once it is done with it. I have the @After going in and cleaning out anything that the test itself doesn't which works great, however it doesn't work so will with working tests (i.e. the test successfully creates and deletes the scheduled instance, and then the @After tries to delete it again and since what it is now selecting is not a deletable instance but it throws an error. This is what I am working with:

@After
public void close()
{
    try
    {
        driver.findElement(By.xpath(delete)).click();
        if (driver.findElement(By.id("syn-delete-btn")).isEnabled()){
            driver.findElement(By.id("syn-delete-btn-btnEl")).click();
            Thread.sleep(989);
            driver.findElement(By.id("button-1006-btnEl")).click();
        }
    }
        catch(Exception e)
        {
            e.printStackTrace();
            Assert.fail();
        }
    driver.close();
}

I thought that perhaps using the isEnabled() would work, however it still considers the delete button (which is grayed out at this point) as enabled. Is there a way to get this if statement to work? It isn't failing with the .click() statement. Clicking the delete button if it goes through brings up a box that asks if you are sure. The test is failing on button-1006-btnEl where the "Yes" button would be on the confirmation panel.

If my code is a little wonky please feel free to post suggestions. I just need this to work.

1

I solved the problem by moving the if statement to the button-1006-btnEl as it is the part throwing the error. I also used the .isDisplayed() method which seems to work out perfectly.

Here is what I did instead

public void close()
    {
        try
        {
            driver.findElement(By.xpath(delete)).click();
            driver.findElement(By.id("syn-delete-btn-btnEl")).click();
            Thread.sleep(989);
            if (driver.findElement(By.id("button-1006-btnEl")).isDisplayed())
            {
                driver.findElement(By.id("button-1006-btnEl")).click();
            }
        }
            catch(Exception e)
            {
                e.printStackTrace();
                Assert.fail();
            }
        driver.close();
    }
0

If I'm reading your question correctly, you want the try block to run only when your test has failed?

If so, then read through this link for JUnit. Instead of the screenshot, you can implement your code on failure.

  • It would be good if you use the link as a reference and write the solution here! – IAmMilinPatel Sep 27 '16 at 3:27
  • Thank you for this link. I find it really helpful and will implement it when I get a chance to and then will upload my code here when it is working. – Senator1891 Sep 27 '16 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.