0

I want to assert multiple text values in a table in a single assert command. I have successfully asserted a single value but how to assert multiple text values. I am new to selenium. So can anyone help me?

4
  • Which assert are you using? Commented Feb 8, 2018 at 7:10
  • I am using:- Assert.assertEquals(string1,string2 ); Commented Feb 8, 2018 at 7:13
  • How does your multiple values look likes , which data structure are you using? Commented Feb 8, 2018 at 7:13
  • Can you post your code trial here Commented Feb 8, 2018 at 7:16

3 Answers 3

1

Selenium have nothing to do assertions. every assertion you have in your code is from the framework/tool that is providing them in pair with selenium. For example if you are using selenium in pari with JUnit then assertions are provided from JUnit.

Second thing, in general testing, it is not a good idea to have multiple assertions in one test, yet, technically it is possible.

Going back to your questions, depending on test framework you are using you can choose which way to do it, e.g. it is JUnit:

assertTrue(someElement1.getText().equals("expected value1"))
assertTrue(someElement2.getText().equals("expected value2"))
assertTrue(someElement3.getText().equals("expected value3"))
assertTrue(someElement4.getText().equals("expected value4"))
Sign up to request clarification or add additional context in comments.

5 Comments

I am not using multiple assertion. I am trying to assert multiple values in a table in a single assert with testng framework.
I understand that, yet, miltiple values assertions is not the best way, in anycase, if you need want to go throw all values of your table and assert them you have to have expected list and then loop via td/tr elements for example and assert tham. Btw It would be easier if you would provide which language and test framework you are using. Also please provde screenshot of the page which contains table or something similar so it will be more clear what you are trying to do
In the table i have 1 row and multiple columns. and I want to access the columns that are dynamic.
multiple lines of assertions are the only way I can see, which you can extract as separate private method and use it in test to make your code nicer.
asserting table values always tricky, but with proper xpath/css and simple loop you can do that
1

You can evaluate all the conditions over one Boolean variable, and after make the assert with the Boolean.

boolean result = someElement1.getText().equals("expected value1") &&
                 someElement2.getText().equals("expected value2") &&
                 someElement3.getText().equals("expected value3") &&
                 someElement4.getText().equals("expected value4");
assertTrue(result)

Comments

1

Selenium doesn't support any kind of the assertion, you have go with the frameworks ex: testNG,JUnit...

I can suggest you 2 methods for asserting multiple values using testNG by assuming you have stored multiple values in ArrayList. You may have to change the logic little based on the data structure you are using.

1.

    List<String> listOne = new ArrayList<String>();

    List<String> listTwo = new ArrayList<String>();

     //I am assuming you have some values in both List 

        int i=0;
        for (String value : listOne) {
            Assert.assertTrue(listTwo.get(i).equals(value),"Expected value+ "+listTwo.get(i)+" not matching with actual value: "+value);
            i++;
        }

2.

Assert.assertEquals(listOne, listTwo);

Hope this will help you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.