views:

35

answers:

1

I want to open a specific URL(relatively easy to achieve in Watir/WatiN), then click on a bookmark/bookmarklet which in turn opens a Javascript window in which certain links then appear. I want to be able to verify links' wording and URLs.

The "problem" is having to use IE (7 & 8) and not Firefox which prevents me from using Selenium IDE for instance, and Watir Recorder seem to be unable to cope with Bookmarklet/Bookmark link.

I've tried using Wintask which accomplished this task partially but I'd very much rather use a regular programming language for this task rather than proprietary tool/scripting language.

+1  A: 

I think that if you want to use a tool such as Watir or Selenium the only solution will be to execute the bookmarklet JavaScript from your test code. Open an ordinal bookmark is the same as navigating to some URL.

You can get the bookmarklet JavaScript from its properties. Let's take a List All Links bookmarklet as example. The JavaScript for is is:

javascript:WN7z=open('','Z6','width=400,height=200,scrollbars,resizable,menubar');DL5e=document.links;with(WN7z.document){write('<base target=_blank>');for(lKi=0;lKi<DL5e.length;lKi++){write(DL5e[lKi].toString().link(DL5e[lKi])+'<br><br>')};void(close())}

From the script you can see that the opened window name is Z6 - we will need it in our code. Unfortunately I do not know Watir much, so my example is in Selenium 2.0 (aka WebDriver) and it is in Java, but I think that the same can be done in Watir:

  WebDriver  driver = new InternetExplorerDriver();
  // Open Google page
  driver.get("http://www.google.com.ua/");
  // Search for something
  WebElement searchField = driver.findElement(By.name("q"));
  searchField.sendKeys("webdriver");
  searchField.submit();   

  // Bookmarklet script, note that javascript: was removed from original booknarklet
  String script = "WN7z=open('','Z6','width=400,height=200,scrollbars,resizable,menubar');DL5e=document.links;with(WN7z.document){write('<base%20target=_blank>');for(lKi=0;lKi<DL5e.length;lKi++){write(DL5e[lKi].toString().link(DL5e[lKi])+'<br><br>')};void(close())}";

  // Execute bookmarklet script
  ((JavascriptExecutor) driver).executeScript(script);

  // Switch to the newly opened window 
  driver.switchTo().window("Z6");

  // Find all the links in the bookmarklet window
  List<WebElement> links = driver.findElements(By.tagName("a"));

  // And list their text - you can do anything with them
  for (WebElement link : links) {
      System.out.println(link.getText());
  }

  driver.quit();

As I understand you are testing the bookmarklet. If you need to examine the links on the page, you can do this with Selenium (and I believe that Watir is also able to do this :)

ZloiAdun
Thanks for such a detailed answer! Some quiestions re:the code.1. window name -it's part of the bookmarklet's JavaScript? So that the window has a permanent name whenever opened?2. I'm indeed testing the bookmarklet's functionality -meaning the contents of the window it opens
akapulko2020
In the example that I've provided, this is the second argument in the open function. And it will be always the same. However I do not know your bookmarklet code so you will need to examine the JavaScript that starts the bookmarklet - it is possible that the window name is generated randomly. The name of the window is the second argument of the window.open function and if you will be calling it from your cade as in my example, you will be able to modify its name
ZloiAdun