Installing chromedriver to run with selenium webdriver on mac

Struggled a bit to have this up and running, so here are some notes!

1. Download chromedriver from here

2 Unzip it

3. Drag it to /usr/bin ( to locate /usr/bin : go to finder, from the go menu go to folder )

You may need to right click on the chrome driver and open it, this is because sometimes on Mavericks if the app is unsigned it doesn’t execute, so you will have to specifically run it.  *

 

4. Restart your system (don’t ask me why 🙂 )

5. Update your properties file to request your webdriver to chrome

*Running an unassigned app on mac

  • To override your security settings and open the app anyway:

    1. In the Finder, locate the app you want to open.

      Don’t use Launchpad to do this. Launchpad doesn’t allow you to access the shortcut menu.

    2. Press the Control key, then click the app icon, then choose Open from the shortcut menu.

    3. Click Open.

CSS selectors and selenium (webdriver)

When I started automation with selenium, I started using xpath, because that was the way to go. Till, when I moved companies, there was this talk of other QA engineers relying on CSS for locating elements from the DOM. I have recently started using CSS locators for various reasons. You can read about it here

I have been basing a lot of my locators on these very cool links

1. Are you ready to use CSS with selenium?
2. Multiple classes and ID’s, CSS- trips and tricks.
3. CSS selectors demystified from Sauceio 
4. Multiple classes and CSS by Maxdesign
5. Can I use this ?  Search
6. Css Rosetta stone and cookbook.
7. And my favorite of all, convert your Xpath to CSS using this link here.[cssify]

Some examples:-

  1. Spaces in classnames: So if you have a class name like this ul class=”A B”, you can do a ul.A.B
  2. Lists can be tricky: Here’s a pointer:
	<li class="shrUPGrid">
           <ul id="sfly_5" class=" shrSG shrSGSelect">
<ul>
	<li style="background-image: <span class=;">url(https://sphotos-a.xx.fbcdn.net.jpg)"></li>
</ul>
<ul>
	<li style="background-image: <span class=;">url(https://sphotos-a.xx.fbcdn.net.jpg)"></li>
</ul>

You can find the Li by

 findElement(By.cssSelector("li.shrUPGrid > ul > li:nth-of-type(""
							+ pictureIndex + ")")).click(); 

where pictureindex is the index of the li

If I am sounding all cryptic please don’t hesitate to drop me a line.

RTE and selenium -uploading image.

I have been spending some quite times trying to upload an image using the YUI RTE. The problems with the upload, is that when the upload button is clicked it calls the native system file uploader (in my case linux) . And this makes selenium access limited. and unapproachable.

I had couple of issues with the RTE which was solved with more of  “google.com” and “meet the dead end-trace back and use another route” method 🙂

Problem: clicking on the upload button would not trigger the file upload overlay.

Solution: Instead of using the click method use the mousedown.

Problem: Inconsistency with speed.

Soution: Not too happy with this, but adjusting the setspeed to slower resolved the problem- this can be problematic when you are trying to figur eout how to run the script on environments that have varied perfomance, not to forget the VM‘s and OS‘s they will be run on.

Problem: How does one pick the file using th enative file selector menu if selenum does not recognize it?

Solution: You can’t. either try and mess with autoit (for IE) or directly type in your file path.

Here is the code: I have again used http://shine.yahoo.com. Please run the test after logging in. I ran it via the IDE so I could control speed using the little bar on the top left. When it comes to running it on the RC, I will need to find the speed it is optimal to run at. Enjoy. As always feel free to comment/email me if you have any suggestions or if you would like to share any nifty tricks.


package com.example.tests;

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.<a class="zem_slink" title="Test cricket" rel="wikipedia" href="http://en.wikipedia.org/wiki/Test_cricket">Test</a>;
import java.util.regex.Pattern;

public class test extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.yahoo.com/");
selenium.start();
}

@Test
public void testTest() throws Exception {
selenium.open("http://shine.yahoo.com/write");
selenium.waitForPageToLoad("");
selenium.mouseDown("xpath=//*[@id=\"yui-gen8\"][@title=\"Image Options\"]");
selenium.focus("xpath=//input[@id=\"insertimage_upload\"]");
selenium.type("xpath=//input[@id=\"insertimage_upload\"]", "<em><strong><enteryourimagefilepathere></strong></em>");
selenium.click("xpath=//*[@id=\"insertimage_upload_btn\"]");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isElementPresent("xpath=/html/body/div/div[2]/div/form/div/div[2]/p")) break; } catch (Exception e) {}
Thread.sleep(1000);
}

selenium.click("xpath=//*[@id=\"imageDone\"]");
}

@After
public void tearDown() throws Exception {
selenium.stop();
}
}

RTE and Selenium.

An interesting issue quick I came across while trying to automate action on the RTE was the inability to directly address the xpath of the text area to write into it.

Here is the source code snippet for the RTE:

<div class="<span class=">yui"</div>
 <iframe = id-"editor_editor" class......>
  <html>
   <head> ............
   </head>;
   <body onload="document.body._rteLoaded = true;">;
    <br _moz_dirty="">
    <br _moz_dirty="">
   </body>
  </html>
 </iframe>

As you can see, there is an iframe involved in the code.

The problem that needed to be solved

While automating the “write” into the text area of the RTE, the xpath was not being recognized. It took me a while to figure out the catch  being the iframe.

The “fix”

The script needs to specifically step into the iframe, address the “write” and then to manipulate any other action outside the iframe, the script needs to select the main window with the select window “null” which is to select the active window.

Here is a front end automation  script in java

package com.example.tests;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;

public class Untitled extends SeleneseTestCase {
@Before
public void setUp() throws Exception {
 selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.yelp.com/");
 selenium.start();
}

@Test
public void testUntitled() throws Exception {
 selenium.open("http://shine.yahoo.com/write");
 selenium.waitForPageToLoad("");
 selenium.selectFrame("xpath=//*[@id=\"editor_editor\"]");
 selenium.typeKeys("xpath=//body", "Yipee! Dude this works!!");
 selenium.selectWindow("");
 selenium.clickAt("xpath=//*[@id=\"blog_title\"]", "");
 selenium.typeKeys("xpath=//*[@id=\"blog_title\"]", "blog");
}

@After
public void tearDown() throws Exception {
 selenium.stop();
 }
}

as you can see from the scripts:

selenium.selectFrame("xpath=//*[@id=\"editor_editor\"]");
selenium.typeKeys("xpath=//body", "Huh");
selenium.selectWindow("");

Steps into by the select frame command, types in with the typeKeys and then selects the active window with the select window “null”

In case you are wondering, I did a quick short script with Selenium IDE, which generates code in Selenese and did a quick translate to java. Go ahead copy paste it, and try it out. Have any more nifty tricks with selenium drop me a note!