Deepak Jindal
Deepak Jindal

Reputation: 17

Soft assert with multiple conditions in selenium

I am trying to get the title of the page, and sometimes they change, but the name is only between 3 names, using the if function below I can get it in selenium.

but I am converting it into soft assert for my test script. basically it should pass the test if the title is between student profile or test or test2.

if(contr0l.getTitle().contentEquals("Student Profile") || contr0l.getTitle().contentEquals("Experiential Learning") || contr0l.getTitle().contentEquals("")){
   System.out.println("CV Link is Correct!"+ '\n' + "Title is " + contr0l.getTitle() + '\n');
}else{
    System.out.println("Title is incorrect, Current Title" + contr0l.getTitle() + '\n');
    }
Softfail.assertEquals(contr0l.getTitle(), ("Student Profile") , ("test")); //  << this line

contr0l.navigate().back();

// Softfail.assertAll();

Upvotes: 0

Views: 2019

Answers (1)

Alexey R.
Alexey R.

Reputation: 8676

You can you hamcrest library to simplify your assertions. This is your dependency:

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>java-hamcrest</artifactId>
    <version>2.0.0.0</version>
</dependency>

This is the example of your test:

package click.webelement.so;

import org.testng.annotations.Test;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;

public class TestTitle {

    @Test
    public void testTitle() {
        String observed = "Title that you have received from driver";
        String[] expectedTitles = {
                "Test 1",
                "Title that you have received from driver",
                "Test 2"
        };
        assertThat(observed, is(in(expectedTitles)));
    }

}

UPD: Using no hamcrest:

package click.webelement.so;

import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.Arrays;

public class Main {

    @Test
    public void testTitle() {
        String observed = "Title that you have received from driver";
        String[] expectedTitles = {
                "Test 1",
                "Title that you have received from driver",
                "Test 2"
        };
        Assert.assertTrue(Arrays.asList(expectedTitles).contains(observed));
    }
}

Upvotes: 2

Related Questions