1

I try to get some website attribute (colour of the cell) and compare in Selenium.

When I put this:

javascript:window.getComputedStyle(document.getElementById("simple_cname"),null).getPropertyValue("background-color");

in Chrome Omnibox, I receive correct answer, but when I, by using storeEval or assertEval try to get this value it does not work correctly.
edit: I put to selenium command like this. I use storeEval and when I echo the value it returns me this command. I use Firefox. I used Chrome just to chech if the command is correct. (it should be "rgb(220, 22, 92)" ) edit2: Yes, the command is ok, but I have a problem with using it in Selenium-IDE tool. It do not returns the value when I use it with storeEval command. log: [info] script is: var test javascript:window.getComputedStyle(document.getElementById("simple_cname"),null).getPropertyValue("background-color"); echo test; [info] Executing: |echo | ${test} | | [info] echo: var test javascript:window.getComputedStyle(document.getElementById("simple_cname"),null).getPropertyValue("background-color"); echo test;


I put to selenium command like this. I use storeEval and when I echo the value it returns me this command. I use Firefox. I used Chrome just to chech if the command is correct. (it should be "rgb(220, 22, 92)" )

Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
mawelpac
  • 35
  • 1
  • 8
  • Are you sure you didn't leave the `javascript:` part in the testcase? In which browser are you trying to run the code? Selenium IDE is for Firefox only, AFAIK... Is there any kind of error, or does it return empty string? – Petr Janeček Jun 27 '12 at 13:56

1 Answers1

0

You need to remove the javascript: part and refer to document as window.document. The command will then look like this:

window.getComputedStyle(window.document.getElementById('simple_cname'),null).getPropertyValue('background-color');

The javascript: part is only needed when running the code from your URL bar, it's redundant anywhere else. Use the Console instead of your Omnibox to run JS commands in Chrome.

The document -> window.document thing is mentioned in the docs under the storeEval section.

Also, note that your script will only work in modern browsers, it will fail in IE < 9. If you're okay with it, fine. If not, Google has the solution.

Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
  • [info] Executing: |storeEval | | test | [info] script is: [info] Executing: |echo | ${test} | | [info] echo: window.getComputedStyle(document.getElementById("simple_cname"),null).getPropertyValue("background-color"); – mawelpac Jul 02 '12 at 09:03
  • Also, no ` – Petr Janeček Jul 02 '12 at 09:05
  • At first I tried without – mawelpac Jul 02 '12 at 09:08
  • Huh. What about `window.getComputedStyle(window.document.getElementById("simple_cname"),null).getPropert‌​yValue("background-color");` ? – Petr Janeček Jul 02 '12 at 09:09
  • [info] script is: window.getComputedStyle(window.document.getElementById("simple_cname"),null).ge‌​tPropert‌​yValue("background-color"); [error] Threw an exception: illegal character – mawelpac Jul 02 '12 at 09:11
  • Now we're getting somewhere :)... `window.getComputedStyle(window.document.getElementById('simple_cname'),null).ge‌​tPropert‌​yValue('background-color');` ? If yes, then Selenium IDE is dumber than I thought... – Petr Janeček Jul 02 '12 at 09:14
  • Worked [here](http://stackoverflow.com/questions/10699039/how-to-convert-color-from-rrr-ggg-bbb-to-rrggbb-by-javascript) and [here](http://stackoverflow.com/questions/10215963/finding-font-weight-value-in-selenium-ide). Try to find differences, or look somewhere else... – Petr Janeček Jul 02 '12 at 09:17
  • 2
    Your js code was good, I rewrited it in Selenium instead of copying and it worked - some encoding problem and one hour of work. Damn. Thanks very much for help! – mawelpac Jul 02 '12 at 09:40