4

I'm working on test automation for a big project and I'm using Phpunit and as a server I'm using Selenium WebDriver.

In the source code I'm using asserts like :

$this->assertContains('Project Title', $this->webDriver->getTitle());

I'm wondering how can I add the if-else condition, that when it's false I can have a popup or to stop the test.

2 Answers 2

0

Solution :

  • For this situation, I have created an array which had only "Project Title"
  • When statement gets executed if its has expected title will continue otherwise execution will gets terminated
  • Provision has been made to add any condition for title, I just need to add in array

$this->assertContains($this->webDriver->getTitle(), array('Project Title');

0

You can use the if-else condition in combination with an assert in your PHPUnit test. Here's an example:

public function testProjectTitle()
{
    // Get the current title of the web page
    $title = $this->webDriver->getTitle();

    // Check if the title contains 'Project Title'
    if (strpos($title, 'Project Title') === false) {
        // Display a popup or log an error message
        echo 'Project title not found';
    } else {
        // Assert that the title contains 'Project Title'
        $this->assertContains('Project Title', $title);
    }
}

In this example, we first get the current title of the web page using $this->webDriver->getTitle(). We then use an if statement to check if the title contains 'Project Title'. If it does not, we can display a popup or log an error message using echo. If the title does contain 'Project Title', we use the assertContains method to assert that the title contains the expected string.

Note that if the assertion fails, PHPUnit will automatically stop the test and mark it as failed. If you want to display a popup or log a message in case of a failed assertion, you can do so in the test case's tearDown method.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.