开发者

How do I programatically click on a bookmarklet that opens a Javascript window and verify the window's contents?

开发者 https://www.devze.com 2023-01-11 02:54 出处:网络
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 开发者_StackOverflow社区in which certain links th

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 开发者_StackOverflow社区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.


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消