1

I created an app that lets users login through my app to Wikipedia, and I achieved this goal with selenium, but I can't find a way to verify either credentials are ok or not.

I tried find by id but since failed condition doesn't display an ID it's not helping.

private void button1_Click(object sender, EventArgs e)
        {

            string BaseUrl = "https://en.wikipedia.org/w/index.php?title=Special:UserLogin&returnto=Main+Page";

             int TimeOut = 30;


        var driver = new FirefoxDriver();



            driver.Navigate().GoToUrl(BaseUrl);



            var loginBox = driver.FindElement(By.Id("wpName1"));

            loginBox.SendKeys("email.address@gmail.com");



            var pwBox = driver.FindElement(By.Id("wpPassword1"));

            pwBox.SendKeys("!SuperSecretpassw0rd");

I would like to know if entered credentials are correct or not.

DebanjanB
  • 118,661
  • 30
  • 168
  • 217

2 Answers2

4

A general approach to this kind of question is to ask yourself, "How can a human see this?" and then replicate this behavior in your test. In your example, how would a human detect that the login is wrong?

A human would see the error message.

Selenium on the other hand only sees the DOM tree. So for Selenium to see the error message, you need to find out where to look in the DOM tree. To do this, open your browser developer tools and find the matching section in the DOM tree:

Human vs. Selenium

With this in mind, a very simple solution is to find the error div that is shown when the credentials are invalid.

var error = driver.findElement(By.className("error"));

Then you can check if the element actually exists and you can use additional Selenium methods to inspect the actual contents of the error message, to see what the error is. If the field is not present then you could assume that the login succeeded. In addition you can use driver.getCurrentUrl() to inspect whether you are still located on the login page, to confirm that you are really logged in.

That being said, if you try to do any serious browser testing you should consider using the page object model (see https://www.toolsqa.com/selenium-webdriver/page-object-model/) so you don't end up with an unmaintainable mess of test cases.

Jan Thomä
  • 12,362
  • 5
  • 46
  • 79
1

As you havn't mentioned the language binding this solution is based on Java.

An elegant approach to validate whether the credentials are valid or not would be to induce a try-catch{} block to look for the element with the error inducing WebDriverWait for the desired visibilityOfElementLocated() and you can use the following Locator Strategies:

  • Code Block:

    public class A_demo 
    {
        public static void main(String[] args) throws Exception 
        {
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
            options.setExperimentalOption("useAutomationExtension", false);
            WebDriver driver = new ChromeDriver(options);
            driver.get("https://en.wikipedia.org/w/index.php?title=Special:UserLogin&returnto=Main+Page");
            new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[name='wpName']"))).sendKeys("Helios.Lucifer@stackoverflow.com");
            driver.findElement(By.cssSelector("input[name='wpPassword']")).sendKeys("!SuperSecretpassw0rd");
            driver.findElement(By.cssSelector("button#wpLoginAttempt")).click();
            try
            {
                new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("form[name='userlogin']>div.error>p")));
                System.out.println("Unsuccessful login attempt");
            }
            catch (TimeoutException e)
            {
                System.out.println("Successful Login.");
            }
            driver.quit();
        }
    }
    
  • Console Output;

    Unsuccessful login attempt
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217