Permalink
Browse files

Really brute force click_link implementation; requires link to be vis…

…ible and bookmarks bar to be shown
  • Loading branch information...
stalcottsmith committed Jul 13, 2011
1 parent f549d14 commit 548a4d5bc58a3850b4f6a24e4de976463bc1701f
Showing with 76 additions and 46 deletions.
  1. +70 −46 lib/safariwatir/scripter.rb
  2. +6 −0 test.rb
View
@@ -431,53 +431,77 @@ def click_element(element = @element)
}| })
end
end
+
+ def calculate_offset(element = @element)
+ execute(element.operate {%|
+ curLeft = 0;
+ curTop = 0;
+ do {
+ curLeft += element.offsetLeft;
+ curTop += element.offsetTop;
+ } while (element = element.offsetParent);
+ return (curLeft + "," + curTop);
+ |}, element)
+ end

This comment has been minimized.

Show comment Hide comment
@stalcottsmith

stalcottsmith Jul 13, 2011

There may be a shorter method to obtain this but I wasn't sure if it was a jquery supplied feature or a feature of safari.

@stalcottsmith

stalcottsmith Jul 13, 2011

There may be a shorter method to obtain this but I wasn't sure if it was a jquery supplied feature or a feature of safari.

+ private :calculate_offset
- def click_link(element = @element)
- click = %/
- function baseTarget() {
- var bases = document.getElementsByTagName('BASE');
- if (bases.length > 0) {
- return bases[0].target;
- } else {
- return;
- }
- }
- function undefinedTarget(target) {
- return target == undefined || target == '';
- }
- function topTarget(target) {
- return undefinedTarget(target) || target == '_top';
- }
- function nextLocation(element) {
- var target = element.target;
- if (undefinedTarget(target) && baseTarget()) {
- top[baseTarget()].location = element.href;
- } else if (topTarget(target)) {
- top.location = element.href;
- } else {
- top[target].location = element.href;
- }
- }
- var click = DOCUMENT.createEvent('HTMLEvents');
- click.initEvent('click', true, true);
- var oEvent = document.createEvent( "MouseEvents");
- oEvent.initMouseEvent('click',true,true,null,1,0,0,0,0,false,false,false,false,0,null);
- if (element.dispatchEvent(oEvent)) {
- } else if (element.onclick) {
- try {
- if (false != element.onclick(click)) {
- nextLocation(element);
- }
- } catch(e) {
- nextLocation(element);
- }
- } else {
- nextLocation(element);
- }
- /
- page_load do
- execute(js.operate(find_link(element), click))
- end
+ def click_link(element = @element)
+ @app.activate
+ # set the bounds of the first window to {0, 0, 1160, 775}
+ @app.windows[1].bounds.set([0, 0, 1160, 775])
+ offset_left, offset_top = calculate_offset.split(",")
+ mac_menu_bar = 20
+ safari_ui = 78
+ click_offset = 5
+ click_x = offset_left.to_i+click_offset
+ click_y = offset_top.to_i + mac_menu_bar + safari_ui + click_offset
+ Appscript.app("System Events").processes["Safari"].click(:at => [click_x, click_y])
+

This comment has been minimized.

Show comment Hide comment
@stalcottsmith

stalcottsmith Jul 13, 2011

Here is the action - the normal method is commented out below.

@stalcottsmith

stalcottsmith Jul 13, 2011

Here is the action - the normal method is commented out below.

+ # click = %/
+ # function baseTarget() {
+ # var bases = document.getElementsByTagName('BASE');
+ # if (bases.length > 0) {
+ # return bases[0].target;
+ # } else {
+ # return;
+ # }
+ # }
+ # function undefinedTarget(target) {
+ # return target == undefined || target == '';
+ # }
+ # function topTarget(target) {
+ # return undefinedTarget(target) || target == '_top';
+ # }
+ # function nextLocation(element) {
+ # var target = element.target;
+ # if (undefinedTarget(target) && baseTarget()) {
+ # top[baseTarget()].location = element.href;
+ # } else if (topTarget(target)) {
+ # top.location = element.href;
+ # } else {
+ # top[target].location = element.href;
+ # }
+ # }
+ # var click = DOCUMENT.createEvent('HTMLEvents');
+ # click.initEvent('click', true, true);
+ # var oEvent = document.createEvent( "MouseEvents");
+ # oEvent.initMouseEvent('click',true,true,null,1,0,0,0,0,false,false,false,false,0,null);

This comment has been minimized.

Show comment Hide comment
@stalcottsmith

stalcottsmith Jul 13, 2011

There are several things we can do here to try to get this initMouseEvent to work with WRIA correctly but it requires experimentation on a working WRIA enabled page.

@stalcottsmith

stalcottsmith Jul 13, 2011

There are several things we can do here to try to get this initMouseEvent to work with WRIA correctly but it requires experimentation on a working WRIA enabled page.

+ # if (element.dispatchEvent(oEvent)) {
+ # } else if (element.onclick) {
+ # try {
+ # if (false != element.onclick(click)) {
+ # nextLocation(element);
+ # }
+ # } catch(e) {
+ # nextLocation(element);
+ # }
+ # } else {
+ # nextLocation(element);
+ # }
+ # /
+ # page_load do
+ # execute(js.operate(find_link(element), click))
+ # end
end
def click_link_jquery(element = @element)
View
@@ -0,0 +1,6 @@
+require 'rubygems'
+$:.unshift("lib")
+require 'safariwatir'
+@browser= Watir::Safari.new
+@browser.goto("http://www.apple.com/startpage/")
+@browser.link(:text, /Mac Basics/).click

0 comments on commit 548a4d5

Please sign in to comment.