Search code examples
seleniumtestngassertiontestng-dataprovider

Testng assert with DataProvider object values


i create a function test false login test but i want to assert with those loop test

here is my sample data

      @DataProvider(name = "Lgindataprovides")
            public Object[][] getData(){
                Object[][] data= {  
    {"[email protected]","12345666"}#email and password wrong (asset text = enter email is not available please register) 
    {"[email protected]","12345666"},#email correct password wrong (assert text = password is wrong)
                return data;
            }

here is my test

   @Test(dataProvider = "Lgindataprovides", dependsOnMethods = "boofoo")
    public void logintest(String email, String passowrd) {
        driver.get("https://dummyurl/login");
        driver.findElement(By.id("email")).sendKeys(email);
        driver.findElement(By.id("password")).sendKeys(passowrd);
        driver.findElement(By.cssSelector("button[type='submit']"));
        Assert.assertEquals(get1stdatatest, "enter email is not available please register")
Assert.assertEquals(get2nderrordatatest, "password is wrong")

    }

buts it fails this test case how can I handle this? what I want is

  1. when this data"[email protected]","12345666" is passed to the login test its should assert with this "enter email is not available please register"

  2. when this data[email protected]","12345666 is passed to the login test its should assert with this "enter email is not available please register"


Solution

  • I see, you wand to check a unique error message per test data pair.

    1. Add the 3rd item to data-provider with the error message text you expecting. So, each email/password will be linked with the error message.
    @DataProvider(name = "Lgindataprovides")
    public Object[][] getData(){
          Object[][] data= {  
                   {"[email protected]","12345666", "error-message-1"},
                   {"[email protected]","12345666", "error-message-2"}
          }
         return data;
    }
    
    1. Add 3rd arg to your test and assert that actual error matches the expected.
    @Test(dataProvider = "Lgindataprovides", dependsOnMethods = "boofoo")
        public void logintest(String email, String passowrd, String expectedError) {
            driver.get("https://dummyurl/login");
            driver.findElement(By.id("email")).sendKeys(email);
            driver.findElement(By.id("password")).sendKeys(passowrd);
            driver.findElement(By.cssSelector("button[type='submit']"));
            Assert.assertEquals(getActualErrorMessage(driver), expectedError)
        }
    
    1. Adjust the method which getting the actual error message text. Your locator for driver.findElement sould find both password or email error messages. You might try to use xpath and search by some class //*[contains(@class, 'some-error-class']. So, you'll get any error message text on the form and it should work.
    private String getActualErrorMessage(WebDriver driver) {
        driver.findElement(By.xpath("...")).getText();
    }
    

    You might also do it inline without creating a new method. Also add some waits/timeouts if you need.