Introduction to the common methods of seven Appium

Source: Internet
Author: User

Text to from: http://www.cnblogs.com/sundalian/p/5629609.html

Because Appium extends the Webdriver protocol, it is possible to use methods provided by Webdriver, such as in the process of webview pages, which can be used entirely in webdriver. Of course, it can be used in native applications as well.

1. Element-related methods

1.1 Click Operation
Webelement button = driver.findelement (by.id ("login"));

Or

Webelement button = Driver.findelementbyid ("login")//then perform a click Operation Button.Click ();

The click () method is called by the element object.

1.2 Clear operation
Webelement username = driver.findelement (by.name ("Username_input"));

Or

Webelement username = driver.findelementname ("Username_input"); Username.clear ();

When this method is executed, the contents of the input box are emptied.

1.3 Getting the value of an element property

GetAttribute (java.lang.String name) this method.

The code is as follows:

Webelement username = driver.findelement (By.id ("U"));

Or

Webelement username = Driver.findelementid ("U"); username. GetAttribute ("XXXXX");

This will get XXXX = "abc", the value of ABC

1.4 Get element text

The source code of the login button:

<button class= "btn btn-major" id= "loginbtn" type= "button" > Login </button>

Webelement provides a gettext () method that can be obtained with the following code:

Webelement login= driver.findelement (by.id ("loginbtn"));

Or

Webelement login= Driver.findelementid ("loginbtn"); Login.gettext ();

This allows you to get the "login" text.

For native apps, the method used is the same, directly referencing the GetText method with the element object.

Whether the 1.5 element is displayed

Determine if the page element is displayed.

The method has a return value type of Boolean, that is, if the element shows a return of true if no display returns false.

Webelement login= driver.findelement (by.id ("loginbtn"));

Or

Webelement login= Driver.findelementid ("loginbtn"),//webview or native app are Universal login.isdisplayed ();

Whether 1.6 elements are selected

In this case, the check box or the radio box, we need to determine whether the box is checked, and this method comes in handy.

Webelement checkbox= driver.findelement (by.id ("checkbox_id"));

Or

Webelement checkbox= Driver.findelementid ("checkbox_id");//webview and native App Universal checkbox.isseelected ();

Checkbox.isseelected (); return value, if checked, returns true if no tick returns false.

Whether the 1.7 element is enabled

Some buttons, which may be grayed out on the page display, are not allowed to be clicked, and this button is not available at this time. So how do we tell if this button can be clicked? Please use the IsEnabled () method

Webelement login= driver.findelement (by.id ("loginbtn"));

Or

Webelement login= Driver.findelementbyid ("loginbtn");//webview and native app Universal login. IsEnabled ();

If True is available, returns False if it is not available.

1.8 Commit Action

On the interface with the form, you can submit without clicking the button, which requires the submit () method

For example, a webview element of the source code:

<button class= "btn btn-major" id= "loginbtn" type= "submit" > Login </button>

This source code needs to be in a form (form), and the type needs to be in the kind of the submit, at which point we can write:

Webelement login= driver.findelement (by.id ("loginbtn"));

Or

Webelement login= Driver.findelementbyid ("loginbtn");//webview and native App submit button Universal login. Submit ();

Processing of IFRAME in 2.webview

Frame is also encountered in the WebView page in the Hybrid app app, as is the case with frame processing in the Web page.

The selenium Webdriver provides a way to enter an IFRAME:

Webdriver Org.openqa.selenium.WebDriver.TargetLocator.frame (String nameorid)

It also provides a way to return the default content:

Webdriver Org.openqa.selenium.WebDriver.TargetLocator.defaultContent ()

Core code:

Driver.switchto (). FRAME ("XXX"); The ID of the IFRAME is passed in

If you want to return to the previous default content, you can use:

Dr.switchto (). Defaultcontent ();

3. Drop-down box selection value

WebView drop-down box action: First locate the drop-down box (element) you want to process, then pass this element into the Select object, then select the drop-down value using the related method in select.

We'll first locate the Select drop-down box (element)

Webelement element_province = driver.findelement (By.id ("province"));

The element is then passed into select

Select province = new Select (element_province);

Then select to manipulate the drop-down box

Province.selectbyindex (0)//province. Selectbyvisibletext ("XXX")

4.alert processing
@Testpublic void Handlealert () throws interruptedexception {webelement showalert= driver.findelement (By.name ("Show Alert "));//Click to eject the Alert window Showalert.click (); Webelement yes=driver.findelement (By.name ("yes"));//Click the Yes button after the alert window disappears Yes.Click ();//Pause 4 seconds to observe the Thread.Sleep (4000);}

Alert we cannot process with the Selenium alert API, we are using a direct way to find the button. This will allow you to find the Yes button and then click the Yes button.

5. Scrolling and sliding

5.1 Scrolling operations

Implementation code:

@Testpublic void Scroll () throws Interruptedexception {//swipe until find to Edriver.scrolltoexact ("E");}

5.2 Slide operation
@Testpublic void Swipe () throws Interruptedexception {thread.sleep (1000); Touchaction taction=new touchaction (driver); taction.press (400,500). Waitaction (MoveTo). Release (). Perform (); Thread.Sleep (4000);}

The core code is:

Touchaction taction=new touchaction (driver);  Taction.press (400,500). Waitaction (+). MoveTo (50,500). Release (). perform ();

Using the Touchaction class, first call the press method, then add the buffer time of the wait action to 800 milliseconds, then move to the coordinates (MOVETO), and when moving to the specified coordinate point, release the hand. Finally, the perform method is called to perform the entire operation.

6. Wait for the element to load

6.1 Hard Wait

Thread.Sleep (int sleeptime);

This method suspends the current driver process for a period of time, and then performs the next operation. One drawback of this approach is that you can't determine how long an element is loaded, and if your sleeptimes is 10 seconds, but the element is loaded in 2 seconds, then the process will continue to wait 8 seconds, resulting in wasted time. Therefore, do not use this method if it is not necessary.

6.2 Smart Wait
public void waitforelementtoload (INT-TimeOut, final by by) {        try {(New webdriverwait (Driver, TimeOut)). Until (New EXPE Ctedcondition<boolean> () {public Boolean ' apply (Webdriver driver) {webelement element = Driver.findelement (by); return element.isdisplayed ();}}); catch (TimeoutException e) {assert.fail ("Timeout!! + Timeout +" seconds after the element has not been found ["+ by +"] ", E);}}

This method has two parameters, timeout is waiting for the element timeout, that is, after this time if the element has not been loaded out of the error. By object, this is the way your element is positioned such as by.id ("login");

This method will find the element at a given timeout, and if the element is found within a time less than timeout, the rest of the time is not waiting for the next operation to be performed directly.

6.3 Setting Wait page loading complete
int pageloadtime = 10;driver.manage (). Timeouts (). Pageloadtimeout (Pageloadtime, timeunit.seconds);

This code, before loading the Driver.get (URL) method, they wait for you for a given time, if the page is not loaded in a given time will be an error, if the load is less than a given time, the rest of the time no longer wait.

7.Spinner drop-down menu selection

Spinner is a drop-down menu component in the Android SDK that allows users to select different values from the drop-down box.

@Testpublic void Testspinner () throws Interruptedexception {webelement spinner=driver.findelement (by.id ("Android:id/ Text1 "));//Click the drop-down box Spinner.click ();//swipe to find until Indiadriver.scrolltoexact (" India "); Webelement optionindia=driver.findelement (By.name ("India"));//click Indiaoptionindia.click ();// Pause 4 Second observation shows Thread.Sleep (4000);}

8.SeekBar Drag Bar action

Implementation code:

@Testpublic void Testseekbar () throws Interruptedexception {//Find the Drag bar webelement Slider=driver.findelementbyid (" Com.android.androidui:id/seekbar1 ");//Gets the start of the drag bar by dragging the x-coordinate of the point int xaxisstartpoint = Slider.getlocation (). GetX ();// Gets the x-coordinate of the end point of the drag bar  = the start x-coordinate + the width of the slider element int xaxisendpoint = Xaxisstartpoint + slider.getsize (). getwidth ();//The y-coordinate of the scroll bar int YAxis = Slider.getlocation (). GetY (); Touchaction act=new touchaction (driver); act.press (Xaxisstartpoint,yaxis). Waitaction (MoveTo, YAxis). Release (). perform ();} @AfterClasspublic void Afterclass () {Driver.closeapp ();}

Introduction to the common methods of seven Appium

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.