How to execute JavaScript in Ruby written Webdriver test?

Is there a known solution to perform Eval (Javascript execution) in Webdriver, Ruby bindings?

The equivalent of the following example in Java.

 WebElement element = driver.findElement(By.id( "foo" ));
 String name = (String) ((JavascriptExecutor) driver).executeScript(
 "return arguments[0].tagName" , element)

Answers:

Answer

The equivalent Ruby would be:

 name = driver.execute_script("return arguments[0].tagName" , element)

(to get the tag name you could also just call element.tag_name)

Answer

Thank you for posting this answer. It helped me a lot. I was googling the whole day to figure out how to execute JavaScript code within Ruby.

@driver.execute_script("arguments[0].scrollIntoView(true);", element )

By doing so, I was able to scroll into the specific element before click event. This might help others a bit.

I had to use JavaScript alternative because for some reason this wasn't: working for me element.location_once_scrolled_into_view

Thanks.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.