Color

Starting in php-webdriver 1.2.0 there is a new class called PHPWebDriver_Support_Color. Its a direct port from the one in the Java bindings originally written by Bob Silverberg.

Why would you be interested in this? Let’s say you are walking that dangerous line of checking computed css values in your scripts.

$e = $this->session->element(PHPWebDriver_WebDriverBy::CSS_SELECTOR, "div");
$background_color = $e->css("background-color");

Well, ends up that it was really challenging to verify this since every browser returned something slightly different.

  • named color: green
  • rgb: rgb(0, 128, 0)
  • rgb expressed as percentages: rgb(0%, 50%, 0%)
  • hex: #008000
  • rgba: rgba(0, 128, 0, 0.5)
  • rgba expressed as percentages: rgba(0%, 50%, 0%, 0.5)
  • rgba expressed as hex: #008000aa
  • hsla: hsla(120,100%,25%,0.5)

Awesome.

Now you might argue that it is up to WebDriver to homogenize these, and you would lose. The browser is doing the right thing and telling you what it thinks the color is. And just who is WebDriver to tell me what representation of color I should use anyways! The nerve!

So its up to the script writer to do the conversion. Or at least up to the script writer to use the appropriate class which will do the conversion for them.

$c = new PHPWebDriver_Support_Color($background_color);
$this->assertEquals("rgba(1, 2, 3, 1)", $c->rgba());

For PHPWebDriver_Support_Color, it supports the formats listed above and will convert them;

  • rgb()
  • rgba()
  • hex()

Now you can have internal consistency in all your scripts — even if the browsers are not returned anything resembling even the loosest definition of consistency.

Edit: I have been informed that WebDriver has indeed standardized on rgba as the color format. Except for HTMLUnit. And any other browser that a vendor creates bindings for and does their own thing…

Post a Comment

Your email is never published nor shared. Required fields are marked *