Skip to content

Commit e0e0a21

Browse files
Fixed the crash in get_source.
AfMSessionController.m (init) - Restored setting Finder as the currentApplication. (allWindows) - Restored the currentApplication as the first item in the returned array. (xmlPageSourceFromRootUIElement:pathMap:xPath:) - Restored using currentApplication as the default rootElement. (xmlPageSourceHelperFromRootUIElement:rootXMLElement:path:pathMap:xPath:) - Fixed the crash by removing an optimization. development.py - Re-added. Got tired of stashing and unstashing. This is not a demo or example, it's a scratchpad.
1 parent 1aec519 commit e0e0a21

File tree

3 files changed

+158
-12
lines changed

3 files changed

+158
-12
lines changed

AppiumForMac/Server/Controller/AfMSessionController.m

+14-11
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ - (id)init
7979

8080
[self setElementIndex:0];
8181
[self setElements:[NSMutableDictionary new]];
82-
// [self setCurrentApplicationName:@"Finder"];
83-
// [self setFinder:[SBApplication applicationWithBundleIdentifier:@"com.apple.finder"]];
82+
[self setCurrentApplicationName:@"Finder"];
83+
[self setFinder:[SBApplication applicationWithBundleIdentifier:@"com.apple.finder"]];
8484
[self setSystemEvents:[SBApplication applicationWithBundleIdentifier:@"com.apple.systemevents"]];
8585
[self setCapabilities:[NSDictionary dictionaryWithObjectsAndKeys:
8686
[Utility version], @"version",
@@ -329,8 +329,8 @@ -(NSArray*) allProcessNames
329329

330330
-(NSArray*) allWindows
331331
{
332-
//return [[NSArray arrayWithObject:self.currentApplication] arrayByAddingObjectsFromArray:[self.currentApplication AXWindows]];
333-
return [self.currentApplication AXWindows];
332+
return [[NSArray arrayWithObject:self.currentApplication] arrayByAddingObjectsFromArray:[self.currentApplication AXWindows]];
333+
// return [self.currentApplication AXWindows];
334334
}
335335

336336
-(NSArray*) allWindowHandles
@@ -903,7 +903,8 @@ -(GDataXMLDocument*)xmlPageSourceFromRootUIElement:(PFUIElement*)rootUIElement p
903903
if (rootUIElement == nil) {
904904
// self.allWindows includes the application uiElement as item 0, and self.currentWindow usually returns the item 0.
905905
// So, rootUIElement is usually the application element here.
906-
rootUIElement = self.currentWindow;
906+
rootUIElement = self.currentApplication;
907+
// rootUIElement = self.currentWindow;
907908

908909
if ([xPath rangeOfString:@"AXMenuBar"].length > 0 || [xPath rangeOfString:@"AXMenuItem"].length > 0) {
909910
// Make sure to search the whole application if looking for a menu item
@@ -980,12 +981,14 @@ -(void)xmlPageSourceHelperFromRootUIElement:(PFUIElement*)rootUIElement rootXMLE
980981
[rootXMLElement addChild:childXMLElement];
981982

982983
// Cheap and dirty performance improvement by checking more often if we are done.
983-
NSError *error;
984-
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithRootElement:rootXMLElement];
985-
NSArray *matches = [doc nodesForXPath:xPath error:&error];
986-
if ([matches count] > 0 && error == nil) {
987-
return;
988-
}
984+
// 20170404 For some reason this crashes during pageSource. Maybe it's a little too dirty!
985+
// Commenting out for now. It's a little slower, but no crash.
986+
// NSError *error;
987+
// GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithRootElement:rootXMLElement];
988+
// NSArray *matches = [doc nodesForXPath:xPath error:&error];
989+
// if ([matches count] > 0 && error == nil) {
990+
// return;
991+
// }
989992
}
990993
}
991994

AppiumForMac/Server/Handlers/AfMHandlers.m

+3-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,9 @@ - (AppiumMacHTTPJSONResponse *)get_source:(NSString*)path
475475
return [self executeWebDriverCommandWithPath:path data:nil onMainThread:YES commandBlock:^(AfMSessionController *session, NSDictionary *commandParams, int *statusCode)
476476
{
477477
// xml page source
478-
return [AppiumMacHTTPJSONResponse responseWithJson:[[NSString alloc]initWithData:[session xmlPageSource].XMLData encoding:NSUTF8StringEncoding] status:kAfMStatusCodeSuccess session:session.sessionId];
478+
NSString *pageSource = [[NSString alloc]initWithData:[session xmlPageSource].XMLData encoding:NSUTF8StringEncoding];
479+
//NSLog(@"%@", pageSource);
480+
return [AppiumMacHTTPJSONResponse responseWithJson:pageSource status:kAfMStatusCodeSuccess session:session.sessionId];
479481

480482
// json page source
481483
//return [AppiumMacHTTPJSONResponse responseWithJson:[session pageSource] status:0 session:session.sessionId];

examples/development.py

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# coding=utf-8
2+
3+
#
4+
# This script is not a demo or example. Use it when trying out AppiumForMac features, python algorithms, and debugging.
5+
#
6+
7+
from selenium import webdriver
8+
from selenium.webdriver import ActionChains
9+
from selenium.webdriver.common.keys import Keys
10+
from random import randint
11+
import time
12+
import utilities
13+
14+
15+
16+
print 'Starting the WebDriver session'
17+
desiredCapabilities = {'platform':'Mac', 'commandDelay':50, 'loopDelay':0, 'implicitTimeout':utilities.defaultImplicitTimeout_sec * 1000, 'mouseMoveSpeed':60, 'diagnosticsDirectoryLocation':'~/Desktop/', 'screenShotOnError':1}
18+
driver = webdriver.Remote( command_executor='http://localhost:4622/wd/hub', desired_capabilities=desiredCapabilities)
19+
20+
print driver.page_source()
21+
22+
#time.sleep(5)
23+
#driver.get("Calculator")
24+
#utilities.selectMenuItemFromMenuBar(driver, "Convert", ["Area…"], "")
25+
# utilities.selectMenuItemFromMenuBar(driver, "Speech", ["Speak Button Pressed"], " and @AXMenuItemMarkChar!=''")
26+
# utilities.selectMenuItemFromMenuBar(driver, "Speech", ["Speak Result"], " and @AXMenuItemMarkChar!=''")
27+
28+
29+
#
30+
#
31+
#
32+
# driver.get("TextEdit")
33+
#
34+
# #utilities.selectMenuItemFromMenuBar(driver, "Format", ["Text", "Writing Direction", " Default"], "")
35+
#
36+
# #utilities.selectMenuItemFromMenuBar(driver, "Format", ["Font", "Hide Fonts"], "")
37+
#
38+
# # Close all floating windows.
39+
# driver.implicitly_wait(0.5)
40+
# while True:
41+
# floatWindowCloseButton = utilities.findElementByXPath(driver, "/AXApplication[@AXTitle='TextEdit']/AXWindow[@AXSubrole='AXFloatingWindow']/AXButton[@AXSubrole='AXCloseButton']");
42+
# if floatWindowCloseButton != None:
43+
# floatWindowCloseButton.click()
44+
# else:
45+
# break
46+
#
47+
# driver.implicitly_wait(30)
48+
#
49+
# # Set up a new window with Rich Text and monospaced font.
50+
# if True:
51+
# ActionChains(driver).key_down(Keys.COMMAND).send_keys("n").key_up(Keys.COMMAND).perform()
52+
# time.sleep(0.5)
53+
#
54+
# window = utilities.findElementByXPath(driver, "/AXApplication[@AXTitle='TextEdit']/AXWindow[0]")
55+
# if window == None:
56+
# print "No front window!"
57+
# quit()
58+
# driver.switch_to.window
59+
#
60+
# driver.set_window_position(50, 50, "current")
61+
# driver.set_window_size(600, 400, "current")
62+
#
63+
# utilities.selectMenuItemFromMenuBar(driver, "Format", ["Make Rich Text"], "")
64+
# utilities.selectMenuItemFromMenuBar(driver, "Format", ["Font", "Show Fonts"], "")
65+
#
66+
# window = utilities.findElementByXPath(driver, "/AXApplication[@AXTitle='TextEdit']/AXWindow[@AXTitle='Fonts']")
67+
# if window == None:
68+
# print "No fonts window!"
69+
# quit()
70+
# driver.switch_to.window
71+
# driver.set_window_position(700, 50, "current")
72+
# driver.set_window_size(600, 400, "current")
73+
#
74+
# # Bring Fonts to the front and make it the key window. We need to click in each table and type-select rows.
75+
# element = utilities.findElementByXPath(driver, "/AXApplication/AXWindow[@AXTitle='Fonts']/AXStaticText[@AXValue='Fonts']")
76+
# ActionChains(driver).click(element).perform()
77+
#
78+
# element = utilities.findElementByXPath(driver, "/AXApplication/AXWindow[@AXTitle='Fonts']/AXSplitGroup[0]/AXScrollArea[0]/AXTable[0]/AXRow[0]")
79+
# ActionChains(driver).click(element).send_keys("Fixed Width").perform()
80+
#
81+
# element = utilities.findElementByXPath(driver, "/AXApplication/AXWindow[@AXTitle='Fonts']/AXSplitGroup[0]/AXScrollArea[2]/AXTable[0]/AXRow[0]")
82+
# ActionChains(driver).click(element).send_keys("Monaco").perform()
83+
#
84+
# element = utilities.findElementByXPath(driver, "/AXApplication/AXWindow[@AXTitle='Fonts']/AXSplitGroup[0]/AXScrollArea[1]/AXTable[0]/AXRow[0]")
85+
# ActionChains(driver).click(element).send_keys("Regular").perform()
86+
#
87+
# element = utilities.findElementByXPath(driver, "/AXApplication/AXWindow[@AXTitle='Fonts']/AXSplitGroup[0]/AXGroup[0]/AXScrollArea[0]/AXTable[0]/AXRow[0]")
88+
# ActionChains(driver).click(element).send_keys("24").perform()
89+
#
90+
# element = utilities.findElementByXPath(driver, "/AXApplication/AXWindow[@AXTitle='Fonts']/AXButton[@AXSubrole='AXCloseButton']")
91+
# ActionChains(driver).click(element).perform()
92+
#
93+
# ActionChains(driver).key_down(Keys.COMMAND).send_keys("a").key_up(Keys.COMMAND).send_keys("to AppiumForMac Welcome!!").perform()
94+
# time.sleep(1)
95+
# ActionChains(driver).send_keys(Keys.RETURN + Keys.RETURN + "oops, sorry, hang on...").perform()
96+
#
97+
#
98+
# textView = utilities.findElementByXPath(driver, "/AXApplication/AXWindow[0]/AXScrollArea[0]/AXTextArea[@AXIdentifier='First Text View']")
99+
# if textView == None:
100+
# quit()
101+
# # Drag the word "Welcome" to the beginning of the line.
102+
# lineStartX = 3
103+
# welcomeLineY = 20
104+
# messageLineY = 80
105+
# welcomeX = 300
106+
#
107+
# ActionChains(driver).move_to_element_with_offset(textView, welcomeX, welcomeLineY).double_click().click_and_hold().move_by_offset(lineStartX - welcomeX, 0).release().perform()
108+
#
109+
# utilities.findElementByXPath(driver, "/AXApplication/AXWindow[0]/AXButton[@AXSubrole='AXCloseButton']").click()
110+
# utilities.findElementByXPath(driver, "/AXApplication/AXWindow[0]/AXSheet[0]/AXButton[@AXTitle='Don’t Save']").click()
111+
112+
113+
114+
115+
#"/AXApplication[@AXTitle='TextEdit']/AXMenuBar[0]/AXMenuBarItem[@AXTitle='Format']/AXMenu[0]/AXMenuItem[@AXTitle='Text']/AXMenu[0]/AXMenuItem[@AXTitle='Writing Direction']/AXMenu[0]/AXMenuItem[@AXTitle=' Default']"
116+
#"/AXApplication/AXMenuBar[0]/AXMenuBarItem[4]/AXMenu[0]/AXMenuItem[1]/AXMenu[0]/AXMenuItem[5]/AXMenu[0]/AXMenuItem[1]"
117+
118+
119+
120+
#utilities.selectMenuItemFromDock(driver, "System Preferences", ["Options", "Show in Finder"], "")
121+
#time.sleep(1)
122+
#ActionChains(driver).key_down(Keys.COMMAND).send_keys("i").key_up(Keys.COMMAND).perform()
123+
#time.sleep(1)
124+
#ActionChains(driver).key_down(Keys.COMMAND).send_keys("w").key_up(Keys.COMMAND).perform()
125+
#ActionChains(driver).key_down(Keys.COMMAND).send_keys("w").key_up(Keys.COMMAND).perform()
126+
127+
#"/AXApplication[@AXTitle='Calculator']/AXMenuBar[0]/AXMenuBarItem[@AXTitle='Apple']/AXMenu[0]/AXMenuItem[@AXTitle='Recent Items']/AXMenu[0]/AXMenuItem[@AXTitle='VMs']"
128+
129+
130+
##'\xe2'
131+
#
132+
##print driver.desired_capabilities
133+
134+
#driver.get("eclipse")
135+
#utilities.selectMenuItemFromMenuBar(driver, "Eclipse", ["Quit Eclipse"], "")
136+
137+
138+
139+
# quit the webdriver instance
140+
print 'Quitting the WebDriver session'
141+
driver.quit()

0 commit comments

Comments
 (0)