0

I am new to selendroid and writing test case for my app.

I have a login page which is displayed only if user is not logged in. I am able to execute test first time and login successfully. I have set noClearData flag true so that I can have data for further process.

Since I am already logged in so home page is open up when I run test second time but code tries to get element by id "email" and throws exception

    org.openqa.selenium.NoSuchElementException: Element was not found.
    io.selendroid.exceptions.NoSuchElementException: Element was not found.
        at io.selendroid.server.handler.FindElement.handle(FindElement.java:58)
        at io.selendroid.server.AndroidServlet.handleRequest(AndroidServlet.java:301)
        at io.selendroid.server.BaseServlet.handleHttpRequest(BaseServlet.java:67)
        at io.selendroid.server.http.ServerHandler.channelRead(ServerHandler.java:50)
        .....

I am checking if element is displayed with below code

    WebElement inputFieldEmail = driver.findElement(By.id("email"));
    inputFieldEmail .isDisplayed() 

but driver.findElement throws exception and test case gets failed. I want to check that if login page is open then only above code is executed.

Please help.

Thanks

Kapil
  • 108
  • 1
  • 12
  • My answer below may be completely wrong depending on what you mean by: "I am able to execute test first time and login successfully. I have set noClearData flag true so that I can have data for further process." Are you running the same single test case multiple times, or are these different test cases in a test suite? If it's the same test that works the first time, then fails the 2nd time, you should probably just clear the data. What are you hoping to gain by keeping the data between tests? – anonygoose May 25 '16 at 22:24
  • Hi anonygoose, Thanks for your reply. App download some data from server when user login into app. I do not want test cases download it each time we run test case. I was running single test case multiple time but now I have created different test cases so that all can be run in on go and its working fine. – Kapil May 27 '16 at 14:18
  • I would do a element count check. int listsize = driver.findelements((By.id("email")).size; And after that I would do an if statement. If (listsize (size == 0){ Do something here} else { Do something else} – Madis Kangro Jun 06 '16 at 07:55

1 Answers1

1

Writing tests where you're not 100% sure where they will be starting from can be a bit of a minefield.

If you do want to do it though, you can surround your findElement with a try/catch.

    try {
        WebElement inputFieldEmail = driver.findElement(By.id("email"));
    } catch(org.openqa.selenium.NoSuchElementException e) {
        // However you want to handle this
    }

That way you can use the try to handle the logic for when it does find the email field, and the catch for when it doesn't.

Alternatively, if you're using a testing framework like JUnit or TestNG you could use the @BeforeClass annotation if you're only going to want to log in once during that entire test suite.

I.e. have an @BeforeClass annotated method do the log in, and then use either @Before or @After to reset to a known point in your application at the start or end of each test case.

anonygoose
  • 741
  • 3
  • 11
  • Thanks for the reply, Yes, I have done the same by surrounding code in try catch block and it allows me to execute next tests. But still it shows errors in logs. But this is not what I was expecting. +1 for your reply. – Kapil May 27 '16 at 14:22