-1

I using POM model and using Cucumber for my Automation.

I am trying to implement a negative scenario for login and I have used the below strategy.

I need to know whether this is the correct approach or I am messing it up somewhere.

I am using xpath to assert Login.

login.feature

    Given User navigates to Site
    And User enters a "<Username>" username
    And User enters a "<Password>" password
    When User clicks on the login button
    Then User should see the failure "<message>"

    Examples:
      | Username | Password   | message|
      | User | Pwd  | //DIV[@class=''][text()='Your login name or password is incorrect.']/../..] |

login.steps

    @Then("^User should see the failure \"([^\"]*)\"$")
    public void user_should_see_the_failure(String arg1 ) throws Throwable {
login_page.assertLoginFailure(arg1);
    }


login.page

    public @FindBy(xpath = "//DIV[@class=''][text()='Your login name or password is incorrect.']/../..")
    WebElement assert_LoginFailure;


    public login_page assertLoginFailure(String arg1) throws Exception {
        Thread.sleep(5000);
        org.testng.Assert.assertEquals(assert_LoginFailure,arg1);
        return new login_page();


    }
moh17
  • 223
  • 1
  • 6
  • 25

2 Answers2

1

The value under message in the DataTable should be the plain text expected when the login is unsuccessful. Ideally should not include any xpath or any selector.

The selector should be defined in the pageobject. Also the selector should be modified to not include the text itself.

This way when the test fails, instead of getting an AssertionError you will get a NoSuchElementException. There is no way of telling if the test failed due to an invalid credential turning out to be valid or the message has been changed on the site.

Thread.sleep() is kind of frowned upon. Rather look at an implicit or explicit wait. Refer to this - http://toolsqa.com/selenium-webdriver/implicit-explicit-n-fluent-wait/

Grasshopper
  • 8,908
  • 2
  • 17
  • 32
  • How do I add my xpaths in the `DataTable` then? – moh17 Jun 20 '18 at 05:28
  • 1
    Why do u need to add xpath in DataTable? Feature files are meant to describe behaviour not technical details. xpaths belong in the pageobject. – Grasshopper Jun 20 '18 at 05:30
  • How do I do the assertion then? I thought I can do it through the which is basically the xpath (or any element locator) for multiple sceanrios – moh17 Jun 20 '18 at 05:32
  • The should just have the text of what is expected. Assert this text from what you get from the site most probably by using getText() on the relevant div or span. – Grasshopper Jun 20 '18 at 05:36
1

This is your assertion :

org.testng.Assert.assertEquals(assert_LoginFailure,arg1);  

where assert_LoginFailure is a web element and arg1 is a String. Now it does not make any sense to compare a string with a web element , does it ?

You have to extract the text present in a web element, something like :

assert_LoginFailure.getText()  

Your assertion will look like this :

org.testng.Assert.assertEquals(assert_LoginFailure.getText(),arg1);  

Hope this will be helpful.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • 1
    Ah thank you!! I just did this using the first answer before you could post! Appreciate your help on my posts :) :) Keep up your good work on Stack oveflow. – moh17 Jun 20 '18 at 05:48