Original Address http://blog.csdn.net/Mandypan/article/details/51396302
Context Switch
Driver.getcontexthandles (); //Get the context of the page
Driver.context ("WEBVIEW");
Driver.context ("Nativeapp");
eg
public void Getresource_android () {
Set<string> Contextset=wd.getcontexthandles ();
for (String Context:contextset) {
SYSTEM.OUT.PRINTLN (context);
}
}
Context Switch
Driver.getcontexthandles ();
Driver.context ("WEBVIEW");
Driver.context ("Navtiveapp");
to view all current Windows:
Set<string> Contextnames=driver.getcontexthandles ();
System.out.print (Contextnames);
Switch to WebView
<pre name= "code" class= "Java" >driver.context ("WEBVIEW");
Driver.findelementbyid ("WD");
Switch to Nativeapp
<pre name= "code" class= "Java" >driver.context ("Native_app");
Demo
/**
* Switch to Native_app or WEBVIEW
* @param swindow window name
*/
private void Switchtowindow (String swindow) {
Logmanager.getlogger (This.getclass ()). info ("Swith to Window:" + Swindow);
set<string> contextnames = Driver.getcontexthandles ();
Logmanager.getlogger (This.getclass ()). info ("Exists windows:" + contextnames.tostring ());
for (String contextname:contextnames) {
if (Contextname.contains (Swindow)) {
Driver.context (Contextname);
Break
}
}
}
Switchtowindow ("WEBVIEW_com.test.android");
Switchtowindow ("Native_app");
Page switching
String Pwindurl = Driver.getcurrenturl (); gets the URL of the current window
Jump to a newly opened window
if (Driver.getwindowhandles (). Size ()! = 2) {fail ();}
For (String handle:driver.getWindowHandles ()) {
Driver.switchto (). window (handle); traverse jumps to each open window
if (driver. Getcurrenturl (). Equals (pwindurl) { If the URL of this window is Pwindurl it indicates that it is the current window
Driver.close (); //Close the current window
}
}
Appium the context Switch