Google Analytics

Search

To search for specific articles you can use advanced Google features. Go to www.google.com and enter "site:darrellgrainger.blogspot.com" before your search terms, e.g.

site:darrellgrainger.blogspot.com CSS selectors

will search for "CSS selectors" but only on my site.


Monday, February 21, 2011

Using Selenium 2.0 with WebDriver and Safari

I've been looking at Selenium 2.0 and writing test cases using WebDriver. Looking at the APIs I see there is a class for Android, iPhone, InternetExplorer, Firefox and Chrome which extends the RemoteWebDriver.

So how do I use Safari? The code to set the search string for Google, using WebDriver, would be:

WebDriver browser = new FirefoxDriver();

browser.get("http://www.google.com");
WebElement input = browser.findElement(By.name("q"));
input.sendKeys("Selenium");

If I wanted to do the same thing using Safari web browser, I could use:

Selenium browser = new DefaultSelenium("localhost", 4444, "*safari", "http://www.google.com");

browser.type(name("q"), "Selenium");

The problem with this is I need to do things differently for Safari. I have to use Selenium 1.0 commands to test Safari and Selenium 2.0 for everything else. So how can I use a browser which was supported in Selenium 1.0 with all the Selenium 2.0 APIs?

The solution is:

Selenium sel = new DefaultSelenium("localhost", 4444, "*safari", baseURL);
CommandExecutor executor = new SeleneseCommandExecutor(sel);
DesiredCapabilities dc = new DesiredCapabilities();
WebDriver browser = new RemoteWebDriver(executor, dc);

browser.get("http://www.google.com");
WebElement input = browser.findElement(By.name("q"));
input.sendKeys("Selenium");

If you look at this block of code, the last 3 lines are the same as the last three lines of the first block of code. Essentially, I add the first three lines then change the WebDriver declaration to a new RemoteWebDriver.

The one thing I found however, on my Mac OS X, if I started the SeleniumServer with setSingleWindow(false) it would fail to work. I run my SeleniumServer inside my Java code using:

private static SeleniumServer ss;
private static RemoteControlConfiguration rcc;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
rcc = new RemoteControlConfiguration();
rcc.setInteractive(true);
rcc.setSingleWindow(true);
rcc.setTimeoutInSeconds(10);
ss = new SeleniumServer(rcc);
ss.start();
}

If you are running the SeleniumServer from the command line, you'll have to look at the command line options to ensure you are running in Single Window mode.

10 comments:

Pavithra said...

Hi,
I tried using the above but driver.get("http://something") is saying "Got result: Undefined value on session sessionvalue"
and because of this my tests are not getting executed.

But interested part when i call driver.get() its loading the url what i am sending but the result is not OK .

Please help me out to resolve this out !!
Did anyone used this and got working; please let me know.

Pavithra said...

Yes now its working; sorry I was actually using *safariproxy and I was getting that error.
But still with *safari, driver.sendKeys is not actually typing the values in the scree; however in console it shows "OK"

Darrell said...

Pavithra, you might want to post a message to http://groups.google.com/group/webdriver. My blog is more a reference then a place to ask for help. If you post to the Google Group you should get a response.

Anonymous said...

any luck with running Safari remotely in grid?

aditi said...

Hello Darrell,

I am really finding hardtime to contact to safari using selenium ver2.20 with win 7.

It would be great if you can help me out.

i am using your code with maven but i am getting this error.

14:09:58,562 INFO [org.openqa.jetty.util.Credential] Checking Resource aliases
14:09:58.566 INFO - Command request: getNewBrowserSession[*safari, http://www.google.com, ] on session null
14:09:58.570 INFO - creating new remote session
14:09:58.775 INFO - Allocated session 58d4863adf1740a88999a9ffb8720aa5 for http://www.google.com, launching..
14:09:59.238 INFO - Launching Safari ...
14:13:20.134 INFO - Command request: getNewBrowserSession[*safari, http://google.com, , ] on session null
14:13:20.134 INFO - creating new remote session
14:13:20.137 INFO - Allocated session 77c11de63c98454f87392a8d0a516dde for http://google.com, launching...
14:13:20.348 INFO - Launching Safari ...

Thanks
Aditi

Darrell said...

I believe around Selenium 2.15 support for Safari, using Selenium 1.0, was no longer working 100% and it only got worse from there. Jason Leyba, a contributor to the WebDriver project has created a Selenium 2.0 specific Safari driver. It was released to the code base last week but they have not made an official release of Selenium since that time.

In other words, the next release of Selenium should include a Safari driver.

Anonymous said...

Hello Darell,

Could you please tell me the code lines to open Safari Browser on Windows through Selenium 2.0 Only.

Payal said...

Hi,

I have to integrate my Automation tool to support Safari browser oin Mac OS.
I have my selenium 2.0 code running on windows for Firefox and Internet Explorer.

Can you please suggest what changes are required for safari mac integration ?

Darrell said...

Best place to ask general questions about Selenium / WebDriver is the WebDriver group at http://groups.google.com/group/webdriver.

man9ar00 said...

Go here for details on using SafariDriver or Safari with WebDriver, the new way.

http://code.google.com/p/selenium/wiki/SafariDriver