开发者

How to get the current Browser in a Selenium RC Test?

开发者 https://www.devze.com 2023-03-20 20:57 出处:网络
I\'m coming up with a problem I still can\'t solve after hours of hard googling : you are my very last chance...

I'm coming up with a problem I still can't solve after hours of hard googling : you are my very last chance...

I have a simple PHP page (let's say "bad_request.php") that respond with a 400 Bad Request status code :

<?php
    header('Status: 400 Bad Request', false, 400);
    header($_SERVER['SERVER_PROTOCOL'].' 400 Bad Request', false, 400);

    exit('Error message');
?>

I have a Selenium RC Test case with a defined set of browsers :

static public $browsers = array(
    array(
        'name'        => 'Google Chrome 12 on Win7',
        'browser'     => '*googlechrome'
    ),
    array(
        'name'    => 'Internet Explorer 9 on Win7',
        'browser' => '*iehta'
    ),
    ...
)

And I have a test method that opens "bad_request.php" and check the status code AND the error message.

The thing is all browsers don't have the same behaviour for a 400 status code : Internet Explorer displays his own error page (and doesn't show "Error message" sent with the exit function) and the other browsers display a simple blank page with "Error message".

This is why I would like my test method to look like this :

public function testBadRequest()
{
    $this->open('bad_request.php');

    /*
     * Test the status code with my own method
     */
    $this->assertStatusCode(400);

    /*
     * Test the page content
     */
    // Case of Internet Explorer
    if (preg_match('`^Internet Explorer`', $this->browserName )) {
        $this->assertTextPresent('HTTP 400');
    }
    // Case of any other browser
    else {
        $this->assertTextPres开发者_如何学Pythonent('Error message');
    }
}

Sadly, browserName is always null and I didn't find any solution to get the current browser in a test method.

Does anybody here have a solution ?


In C# (different than your PHP question, but it may help someone who comes across this question like I did), I used driver.Capabilities.BrowserName where driver is the OpenQA.Selenium.Remote.RemoteWebDriver of the current test run.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号