Introduction to the common methods of eight Appium

Source: Internet
Author: User
Tags appium

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 performs 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 apps are common 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 submission buttons are common 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
  @Test  public  void  Handlealert ()  interruptedexception {webelement Showalert  = driver.findelement (By.name ("Show Alert" )); //  Click to pop up the alert window  showalert.click (); Webelement Yes  =driver.findelement (by.name ("Yes"  //  Click the Yes button and the alert window disappears  yes.click ();  //  pause 4 seconds observation display  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:

@Test  Public void throws interruptedexception {// swipe until find to edriver.scrolltoexact ("E");}

5.2 Slide operation
@Test  Public void throws interruptedexception {thread.sleep (+); Touchaction taction=new  touchaction (driver); Taction.press (400,500). Waitaction (+). MoveTo (50,500). 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 voidWaitforelementtoload (intTimeOut,Finalby by) {Try {(NewWebdriverwait (Driver, TimeOut)). Until (NewExpectedcondition<boolean>() { PublicBoolean Apply (webdriver driver) {webelement element=driver.findelement (by);returnelement.isdisplayed ();}});} Catch(TimeoutException e) {assert.fail ("Timeout!!" + timeout + "seconds not found element [" + 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 = ten;d river.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.

@Test Public voidTestspinner ()throwsinterruptedexception {webelement spinner=driver.findelement (By.id ("Android:id/text1"));//Click the drop-down boxSpinner.click ();//swipe to find it until you pick it up IndiaDriver.scrolltoexact ("India"); Webelement Optionindia=driver.findelement (By.name ("India"));//Click IndiaOptionindia.click ();//Pause 4 Second observation displayThread.Sleep (4000);}

8.SeekBar Drag Bar action

Implementation code:

@Test Public voidTestseekbar ()throwsinterruptedexception {//Find the Drag barwebelement Slider=driver.findelementbyid ("Com.android.androidui:id/seekbar1");//gets the x-coordinate of the start drag point of the drag barintXaxisstartpoint =slider.getlocation (). GetX ();//gets the x coordinate of the end point of the drag bar = start x coordinate + width of the slider elementintXaxisendpoint = Xaxisstartpoint +slider.getsize (). getwidth ();//the y-coordinate of the scroll barintYAxis =slider.getlocation (). GetY (); Touchaction Act=Newtouchaction (driver); act.press (Xaxisstartpoint,yaxis). Waitaction (). MoveTo (xAxisEndPoint-1, YAxis). Release (). perform ();} @AfterClass Public voidAfterclass () {Driver.closeapp ();}

Introduction to the common methods of eight 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.