Is it possible to execute Javascript in the context of a page with Conductor Selenium framework? - selenium

I am using Conductor to control PhantomJS via Selenium/Webdriver. PhantomJS has a function evaluateJavascript.
Is it possible to execute Javascript in the context of a page with Conductor? If so, how?

I have not tried it, but I came across a similar problem with HtmlUnit.
I solved it by checking out the conductor framework GitHub project and I modified the Locomotive constructor to get the WebClient from the WebDriver. So you could try to cast the WebDriver to PhantomJSDriver and then access the internal methods like "executeScript", "executePhantomJS":
case PHANTOMJS:
capabilities = DesiredCapabilities.phantomjs();
if (isLocal)
try {
driver = new PhantomJSDriver(capabilities);
// Cast to PhantomJSDriver
((PhantomJSDriver)driver).executeScript(...)
}

Related

How to find the driver instance in QAF framework

During execution I want to know which browser currently the script is running and do some actions based on the browser the script is running.
I am using
Option 1:
if (new QAFExtendedWebDriver().getUnderLayingDriver().equals("ChromeDriver")) {
// Do`enter code here` some thing if this is a chrome browser
}
else if (new QAFExtendedWebDriver().getUnderLayingDriver() instanceof FirefoxDriver) {
}
Thsi option is not working during my excution. What is the way to know which browser I am in ?
Below are few examples:
//to get driver name for this thread which provided using driver.name
String drivername = TestBaseProvider.instance().get().getDriverName();
//if running in browser, browser name for this thread, will not be avialable for mobile native or hybrid apps
String browserName =getDriver().getCapabilities().getBrowserName();
//underlying driver class name, which can be remote driver if you are using remote driver
String driverClassname = getDriver().getUnderLayingDriver().getClass().getSimpleName();
you didn't provided details about what you are trying to achieve. If you want to do something when browser opens, you can utilize driver listener.
If you are using bdd and you have platform/browser specific code, instead of using if else you can have separate step implementation for each platform/browser in different package and load step provider package accordingly, for example:
step.provider.pkg=com.exmple.steps.common;com.exmple.steps.chrome

Selenium Actions.movetoElement - org.openqa.selenium.UnsupportedCommandException

I have a scenario where I have to hover over a menu link and click on the drop down sub menus. The code that I'm using is below:
public void changeLanguageTest() throws InterruptedException
{
WebElement LanguageMenu = driver.findElement(By.cssSelector(".change-language>button"));
action.moveToElement(LanguageMenu);
WebElement mongolianLang = driver.findElement(By.cssSelector(".change-language>ol>li:nth-child(3)>a"));
action.moveToElement(mongolianLang).click().build().perform();
Thread.sleep(3000L);
}
But when I run this code, It fails with an error message :-
org.openqa.selenium.UnsupportedCommandException: POST
/session/3077f893-d9ab-487d-b09f-c5bcd135ea31/moveto did not match a
known command
I tried below mentioned code too but no success and same error occurred.
BaseClass.action.moveToElement(LanguageMenu).moveToElement(mongolianLang).click().build().perform();
I'm using webdriver v2.53 and running it on FF v47.0.1.
As a bug logged here geckodriver does not yet implement actions. The actions we will implement are those being defined right now in the W3C WebDriver standard and not those of Selenium.
Selenium has said they will provide a Selenium-to-W3C-WebDriver shim for actions, but this may take some time to produce after we have made our implementation. Implementation in geckodriver/Marionette has not yet started.
As mentioned here from v0.12.0 of geckodriver, Implemented routing for new actions API, but it too is not yet fully implemented in Marionette
You should upgrade your geckodriver.

Mink and Sahi timing out on getContent

I've written a website scraper for use in a project.
I'm controlling Firefox through Sahi using Mink to visit each site and interact with any elements where necessary. I've managed to get this working perfectly on all sites I've tried except for one...
I'm trying to get the markup from https://www.o2.co.uk/shop/phones/
I'm using the exact same code for this page, as I have for all others:
// Configure driver
$this->driver = new \Behat\Mink\Driver\SahiDriver('firefox',
new \Behat\SahiClient\Client(
new \Behat\SahiClient\Connection(null, CRAWL_SERVER, 9999)
)
);
// Init session:
$this->session = new \Behat\Mink\Session($this->driver);
// Start session:
$this->session->start();
// Open the url
$this->session->visit($config['url']);
// Get the markup from the page
$markup = $this->session->getPage()->getContent();
When I use this code to attempt to get the markup from https://www.o2.co.uk/shop/phones/ Mink just seems to hang, waiting for something to happen.
It would seem that maybe something on this page is preventing either Sahi or Mink from returning the markup. I've also tried running other functions instead of getContent(), such as $this->session->wait(2000); and attempting to search through getPage using the find command.
If anyone has any idea as to why this is happening I would be very interested in finding out why and how I can make this work.
tl;dr
Why is Mink/Sahi timing out on this site?

Element not being added when running test through webdriver

I am working on writing a story for a bdd framework which uses jbehave/selenium/webdriver and am having a problem where the test encounters an error while running the story but appears to be fine when running manually. I'm having a problem where javascript for the functionality I'm testing behaves slightly different when I'm running tests manually on firefox vs through selenium web driver on the same system/version of firefox and this difference is causing a js error.
I've debugged and basically the root of the problem appears to be that var request_XML_container = $('div_appendpoint_id'); returns something different when I'm running the test manually vs when I run through the bdd framework.
var request_XML_container = $('div_appendpoint_id');
request_XML_container.innerHTML = encoded_xml_from_request;
var pos = method_to_get_position('id_of_place_div_should_be_appended_to');
// JS exception is thrown saying that style is not defined **ONLY**
// when running through web driver. Running test manually on
// same system and same browser works fine.
request_XML_container.style.left = (pos[0] - 300) + 'px';
request_XML_container.style.top = (pos[1] + 25) + 'px';
request_XML_container.style.display = "block";
Why this would work fine when running manually that var request_XML_container = $('div_appendpoint_id'); would return an item with style defined, but when running through webdriver that the style attribute of the element would not be defined?
UPDATE: I had originally thought that this was updating an iframe, but I read the markup wrong and the iframe I saw is a sibling - not a parent - of the element where the response is being appended to. I'm trying to simply append the response to a div. To be honest, this only makes things more confusing as grabbing a div by id should be pretty straight forward and I'm now sure why webdriver would be producing a different return element in this situation.
UPDATE 2: Steps to reproduce and information about the system I'm on:
Use webdriver to navigate to this url: http://fiddle.jshell.net/C3VB5/11/show/
Have webdriver click the button. It should not work
Run your test again, but pause put a breakpoint at your code to click the driver
Click the button on the browser that webdriver opened. It should not work
Refresh the browser page on the browser that webdriver opened. Now, it should work.
System details:
OS : OS X 10.8.5 (12F37)
IDE : Eclipse Kepler: Build id: 20130614-0229
Browser (used manually and by webdriver) : Firefox 23.0.1
Selenium version: 2.35.0
UPDATE 3: I have provided this maven project on github to aid in reproducing: https://github.com/dkwestbr/WebdriverBug/tree/master/Webdriver
Synopsis/tl:dr; Basically, in certain situations it appears as though webdriver is overwriting the '$()' javascript method with a method that does not return an HTMLElement with innerHTML or style defined (among other things). This post details the issue and how to reproduce.
I have opened this ticket to track the issue: https://code.google.com/p/selenium/issues/detail?id=6287&thanks=6287&ts=1379519170
I have confirmed that this is a bug with the Thucydides framework (understandable since they still aren't at a 1.0 release).
Issue can be tracked here: https://java.net/jira/browse/THUCYDIDES-203

WebDriver - is it possible to use Firefox or IE javascript engine when using HtmlUnitDriver using .NET Selenium bindings?

I am running into inconsistent behaviour when running my test using DesiredCapabilities.Firefox() vs. DesiredCapabilities.HtmlUnitWithJavaScript().
For the most part, my issue lies with DOM elements not appearing on the page when they should be there.
I've used a number of different strategies for "Wait"-ing for said element, but still, HtmlUnit driver seems to think that the element is not visible.
With that said, I am now asking if it's possible to run HtmlUnit providing a Firefox or IE handle; similar to how Java's HtmlUnitDriver implementation works:
HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3);
try WebDriver driver = new HtmlUnitDriver(DesiredCapabilities.firefox());