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?
-
Which assert are you using?Pradeep hebbar– Pradeep hebbar2018-02-08 07:10:18 +00:00Commented Feb 8, 2018 at 7:10
-
I am using:- Assert.assertEquals(string1,string2 );Gautam Bothra– Gautam Bothra2018-02-08 07:13:17 +00:00Commented Feb 8, 2018 at 7:13
-
How does your multiple values look likes , which data structure are you using?Pradeep hebbar– Pradeep hebbar2018-02-08 07:13:30 +00:00Commented Feb 8, 2018 at 7:13
-
Can you post your code trial herePradeep hebbar– Pradeep hebbar2018-02-08 07:16:41 +00:00Commented Feb 8, 2018 at 7:16
3 Answers
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"))
5 Comments
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
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