1

I am finding a hard time automating a basic UI test case. Using Selenium for the first time.

I am trying to write some UI test cases for an application which has User Login module with a username and password. After login, there can be two scenarios as usual : Successful Login and Incorrect password.

On Successful Login the Application URL changes. E.g. www.abc.com/AA to www.abc.com/BB.

On Incorrect Password, page REFRESHES once and then show an error message on the same page.

Now the problem is , I am able to open the page, enter U/N and Pswd through Selenium but I face issue after clicking 'Login' button when page redirect happens/refreshes.

Webdriver is unable to find any element on the page. I have already checked there is no IFrame on the page.

I am using C# Selenium Web Driver in a .net Console application. Citing the code for refrnce:

       var options = new InternetExplorerOptions()
        {
            InitialBrowserUrl = URL,
            IntroduceInstabilityByIgnoringProtectedModeSettings = true,
        };
        var driver = new InternetExplorerDriver("www.abc.com/AA", options);
        driver.Navigate();

        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
        var username = wait.Until<IWebElement>((d) =>
        {
            return d.FindElement(By.Id("userName"));
        });           
        username.SendKeys("User1");

        var password = driver.FindElement(By.Id("userPwd"));
        password.SendKeys("Password@123");

// Login button pressed in the next line          
        password.SendKeys(Keys.Enter);

//WAIT FOR RESULT
  WebDriverWait waiter = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

// This below line Shows error "Unable to find Element..."
        IWebElement categoryList = wait.Until<IWebElement>((d) =>
        {
            return d.FindElement(By.Id("SAMPLE-DROPDOWN-ID"));
        });           

        categoryList.SendKeys("1");
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
CuriousBuddy
  • 179
  • 1
  • 8
  • 21
  • Try printing the getPageSource() after clicking and waiting for the next page to load completely. – Amrit Mar 16 '15 at 09:51
  • Thanks Amrit..but there is nothing like "getPageSource" in 'InternetExplorerDriver' object. The availables are GetHashCode(), GetScreenshot() and GetType(). Can you help with a small snippet. – CuriousBuddy Mar 16 '15 at 10:21
  • http://stackoverflow.com/questions/17734065/internetexplorerdriver-getpagesource-is-returning-a-different-string-than-fire – Amrit Mar 16 '15 at 10:24
  • How will this help..Can you please elaborate. it is just giving the source of the last loaded page. My concern is why I am not able to find and click elements after a page refresh, or, after a page redirection. – CuriousBuddy Mar 16 '15 at 11:09
  • that's the point - it should not give the source of last page if you have navigated to next page! Ar you able to inspect elements on the next page? – Amrit Mar 16 '15 at 11:21
  • SAMPLE-DROPDOWN-ID should be loaded in page source before you can call find element – Amrit Mar 16 '15 at 11:23
  • Got your point !! Should I put a wait before getting the page source as it might take some time for refresh/new page load to take place. But is it good to check PageSource as I am very much able to Inspect element on the newly loaded page and based on that only I have got the SAMPLE-DROPDOWN-ID. – CuriousBuddy Mar 16 '15 at 11:49
  • yes - add wait - use examples from this post http://stackoverflow.com/questions/21339339/how-to-add-custom-expectedconditions-for-selenium – Amrit Mar 16 '15 at 12:39

2 Answers2

1

You can try having a conditional loop with a check on the URL that wait for an X period of time till it contains abc.com/BBB then check the visibility of the web element on Successful case, else check the error message. This will also easier for you to debug the issue and make your test less prone to errors.

EDIT: Here's the snippet to your query (Wait until URL changes) I am not well versed with the C# style, but here it is in Java, you can apply the same logic -

public static String start_URL = "http://example.com/";

public static WebDriver driver = null;
public static WebDriverWait wait = new WebDriverWait(driver, 10);
public static String prev_URL = null;
public static String curr_URL = null;

public static void main(String[] args) {

    driver = new FirefoxDriver();

    driver.navigate().to(start_URL);

    prev_URL = driver.getCurrentUrl();

    //do login operation - enter user name and password

    ExpectedCondition e = new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver driverNew) {
            return (driverNew.getCurrentUrl() != prev_URL);
        }
    };

    wait.until(e);
    curr_URL = driver.getCurrentUrl();
    System.out.println(curr_URL);

}
DRVaya
  • 443
  • 4
  • 13
1

The problem is that Selenium doesn't know that the page has refreshed, and it is looking for the new control in the "previous" page.

Just using "send keys" doesn't allow Selenium to figure out that a page transition has occurred, you could just be typing paragraphs of text into a text area.

If there is a login button then you should "click" it, and Selenium will then expect a page reload. Otherwise you'll have to force Selenium to manually reload the page.

Bigwave
  • 2,166
  • 1
  • 17
  • 28
  • Thanks @Bigwave. This is an eye-opener, if Selenium works that way. Thanks. Will surely try this and let you know the outcome. Can you please tell me how to reload the page, and will IE-Driver object will automatically get the data of the new page. – CuriousBuddy Mar 16 '15 at 15:14