Unable to inject javascript using selenium 3 in firefox - selenium

I am injecting java script variables using selenium and retrieving it for verification.
Below is my sample code, which was working fine with selenium version 2.53.1.
When I upgraded to selenium 3 and started to use gecko driver for firefox, it throws exception when I am retrieving value.
driver.executeScript("globalVar='Amit';");
Object result = driver.executeScript("return globalVar");
System.out.println(result.toString());
Exception:
org.openqa.selenium.JavascriptException: ReferenceError: globalVar is not defined
In selenium 3 same code also working for Chrome.
Am I missing anything here? Or is there any capabilities added to allow such things in Firefox/Gecko driver?

The variables you set in a script that you execute are not global - they are "sitting" in the scope of the executed function. If you want to have a global variable that you want to access across multiple executed scripts, you have to use one of the available global objects, e.g. window:
driver.executeScript("window.globalVar = 'Amit';");
Object result = driver.executeScript("return window.globalVar");
System.out.println(result.toString());

Related

Browser not quitting when called from another scenario using Karate

I have a scenario which is a series of rest api calls but in the middle is a section that executes a few steps within a chrome browser. The browser steps are common to another scenario so I tried to extract the browser steps into a separate feature that could then be called from multiple scenarios.
When the main scenario executes it executes the browser feature but fails to auto-close the browser after execution. I read in the documentation "Karate will close the browser automatically after a Scenario unless the driver instance was created before entering the Scenario" . The configure driver code is in the callable scenario.
I also tried caling quit() but this resulted in the error: "The forked VM terminated without properly saying goodbye. VM crash or System.exit called?"
Does anyone know how I can ensure the browser closes in this circumstance?
UPDATE: As suggested by #PeterThomas I started to craft a full example to replicate this when I discovered that to replicate is actually quite simple.
If the UI feature is called like this then the browser is closed after execution:
* call read('classpath:/ui/callable/GoogleSearch.feature')
If called like this then the browser remains open:
* def result = call read('classpath:/ui/callable/GoogleSearch.feature')
My UI scenario scrapes a value from a web page which I then stored within a '* def ticket' within the called feature. I was hoping to access it via result.ticket. As I am unable to do this I am successfully using the following:
* def extractedTicket = { value: '' }
* call read('classpath:/ui/callable/GoogleSearch.feature')
* def ticket = extractedTicket.value
And within the called feature:
* set extractedTicket.value = karate.extract(val, '.ticket=(.*?)&', 1)
First, I think you should provide a way to replicate this, so that we can investigate and fix this for everyone. Please follow this process: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue
That said, maybe for just getting Chrome to do a few steps you should just use the Java API - and you can call it from wherever you want, even within a feature file using Java interop: https://github.com/karatelabs/karate#java-api
Also see if this answer gives you any pointers: https://stackoverflow.com/a/60387907/143475

How to use webdriver using the dart library?

I am having trouble getting started with the webdriver dart library.
I was hoping for some simple examples.
I do have the seleniumn server standalone running in the background.
I am very new to dart and very experienced with ruby and watir-webdriver.
I was expecting something similar to the code below
import 'package:webdriver/webdriver.dart';
main() {
var url = "http://google.com";
var driver = new WebDriver();
b = driver.newSession(browser:'firefox');
b.getUrl(url);
}
But the error I am getting is
Unhandled exception:
No constructor 'WebDriver' declared in class 'WebDriver'.
Looking at the source
class WebDriver extends WebDriverBase {
WebDriver(host, port, path) : super(host, port, path);
So it seems like the constructor is there; and the defaults are in the WebDriverBase to go to the remote server. What am I doing wrong? I have scoured the internet trying to find simple examples with no luck
Currently, there are known issues with local and session storage, script execution, and log access.
To use these bindings, the Selenium standalone server must be running. You can download it at http://code.google.com/p/selenium/downloads/list.
There are a number of commands that use ids to access page elements. These ids are not the HTML ids; they are opaque ids internal to WebDriver. To get the id for an element you would first need to do a search, get the results, and extract the WebDriver id from the returned Map using the 'ELEMENT' key. see http://commondatastorage.googleapis.com/dartlang-api-docs/13991/webdriver.html

Migrating to Webdriver from Selenium RC

I am migrating from RC to webdriver.
In my existing project I use methods from the Selenium Class like
selenium.click()
selenium.type()
etc.
Do I need to change these to the equivalent webdriver commands, or is there a way i can still use these commands?
I use firefox 12, Eclipse IDE
There is the WebDriverBackedSelenium. Essentially this is a bridge between the RC API and WebDriver API. This will do what you are after, there will be some modification to code, but majority will still be the same. It gives you the flexibility of the WebDriver itself, while keeping old code the same.
It is highly recommended to fully convert your solution to use the WebDriver API directly.
The WebDriver API is constantly being updated, worked on and supported.
The RC API and the "RC-WebDriver-Bridge" (WebDriverBackedSelenium) won't be.
Page on WebDriverBackedSelenium exists here:
http://seleniumhq.org/docs/03_webdriver.html#alternative-back-ends-mixing-webdriver-and-rc-technologies
Sample usage to create a new instance of Firefox:
var driver = new FirefoxDriver();
var selenium = new WebDriverBackedSelenium(driver, baseUrl);
selenium.open("http://www.google.com");
selenium.type("name=q", "cheese");
selenium.click("name=btnG");
selenium.stop();
After creating a WebDriverBackedSelenium instance with a given Driver, one does not have to call start() - as the creation of the Driver already started the session. At the end of the test, stop() should be called instead of the Driver's quit() method.
This is more similar to WebDriver's behaviour - as creating a Driver instance starts a session, yet it has to be terminated explicitly with a call to quit().

what is the difference between selenium.click and driver.click

I have recorded a scenario in Selenium IDE and exported it as a Junit4 Webdriver backed code.
There is a command which uses selenium object and the same thing could be done by driver object.
So I am not able to understand which one to use and when
E.g :
selenium.click("id=gen_info") can also be implemented by
driver.findElement(By.id("gen_info")).click();
Yes I do have a option to have a driver object of specific web browser but then the same thing could be done by using selenium object also.
I suppose that by selenium click u mean something like this:
WebDriver driver = new FirefoxDriver();
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
WebDriverBackedSelenium allows those who have test suites using the Selenium-RC to migrate to WebDriver. However it doesn't implement all methods.
In this particular case it should work the same, though WebDriverBacked may be slower

Getting error in selenium test cases

I'm running somebody written selenium test cases in my system.It showing the few errors like
> [error] Actual value 'null' did not match '[object Object]'
> [error] Threw an exception: this.browserbot.getUserWindow().map is undefined
> [error] Threw an exception: this.browserbot.getUserWindow().map is undefined
Is it the problem with selenium ide version which I'm using or other? I'm using Selenium 1.6.0
This issue is coming because you are trying to get hold of a window , which is not matching to the value you are passing.
So for this you need to investigate more on your locators. Also you can start using a new tool which can find automatically the locators for you , which is Selenium Builder. Hope following links will help you.
http://khyatisehgal.wordpress.com/2014/05/26/selenium-builder-exporting-and-execution/
http://khyatisehgal.wordpress.com/2014/05/25/selenium-builder/
Those errors you're getting are not because of Selenium version, but because the logic of your application changed and the tests expect different results from various actions.
The only thing you can do is to go through your tests, discover what were they trying to assert / achieve / test (you do have your testcases documented, right?) and if their current behaviour is wrong, fix them.
The other possibility is that your tests are ok, just the application began to behave differently (against the spec) and needs to be fixed. But from the context, I'd say that your unmaintained tests broke.