0

我正在使用混合应用程序并在Ruby中使用Appium + Selenium Webdriver编写测试。Selenium Webdriver(Appium) - 在混合应用程序(iOS)中切换到UIWebview

我开始测试一些文本编辑+点击一个按钮来打开UIWebview(到目前为止一切正常)。问题是当UIWebview打开时 - 我无法访问它(当我尝试点击一个html元素时(我正在使用Appium检查器来查找元素并记录我的Ruby测试),它立即关闭。我知道我有切换到一个UIWebView(因为我发现here),但我不能让它工作

代码示例:

require 'rubygems' 
require 'selenium-webdriver' 
capabilities = { 
    'browserName' => 'iOS', 
    'platform' => 'Mac', 
    'version' => '7.1', 
    'device' => 'iPhone Retina (4-inch)', 
    'app' => '/Users/{my user here}/Library/Developer/Xcode/DerivedData/{path here}/Build/Products/Debug-iphonesimulator/SDK.app' 
} 

server_url = "http://127.0.0.1:4723/wd/hub" 

@wd = Selenium::WebDriver.for(:remote, :desired_capabilities => capabilities, :url => server_url) 
# ... 
# Do all kind of native actions here 
# ... 

@wd.find_element(:name, "showWebviewButton").click 
@wd.manage.timeouts.implicit_wait = 30 # seconds 
# ??? 
# How do I switch to my UIWebview here??? 
# (cannot access html elements here with @wd.find_element(:name, "htmlElement")) 
# ??? 

@wd.quit 

编辑: 使用Appium检查我发现我的UIWebView是“窗口(1)“,所以我试过了:

@wd.switch_to.window(1) 

这给我的错误:

A request to switch to a different window could not be satisfied because the window could not be found 

(加载的UIWebView之前抛出的错误)

回答

0

终于为我工作的唯一解决方案(使用Appium 1.2)是(从here取,写在node.js中)

// javascript 
// assuming we have an initialized `driver` object for an app 
driver 
    .contexts().then(function (contexts) { // get list of available views. Returns array: ["NATIVE_APP","WEBVIEW_1"] 
     return driver.context(contexts[1]); // choose the webview context 
    }) 

    // do some web testing 
    .elementsByCss('.green_button').click() 

    .context('NATIVE_APP') // leave webview context 

    // do more native stuff here if we want 

    .quit() // stop webdrivage 

在上面的link中,您可以找到用其他语言编写的解决方案。

2

看来,它加载之前您切换到的WebView。请暂停脚本一段时间,然后在出现WebView后切换到WebView。

+0

好的,tnx。这似乎是合理的,但正如你所看到的 - 我打算在打开Webview之前等待30秒。你有什么想法我在这里做错了什么? TNX! –

0

我已经尝试了以下在Java中,并为我工作得很好,你可能需要找到相同的红宝石版本。

driver.switchTo().window("WEBVIEW"); 

尝试这种

@wd.switch_to.window("WEBVIEW") // i am not sure about the syntax 
0

您需要访问该元素如下所示

findElement(By.xpath( “//输入[@名称= '的firstName']”))

相关问题