Home > Java > javaTutorial > body text

Introduction to methods of obtaining element status in Appium and Java

不言
Release: 2018-10-12 14:29:28
forward
3191 people have browsed it

This article brings you an introduction to the methods of obtaining element status in Appium and Java. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. We often use the attributes of the

element. After locating an element, we sometimes need to use the text value, className, resource-id, checked, etc. of this element.

Generally standard attributes can be obtained through get_attribute("attribute name"). Let's take a look at how to obtain the elements in the screenshot below. Viewed from top to bottom.

Let’s start with text. We first locate this element through xpath.

public class UIdemoTest {
    private AndroidDriver driver;

    @Before
    public void setUp() throws Exception {
        //设置并启动“app”
        File classpathRoot = new File(System.getProperty("user.dir"));
        File appDir = new File(classpathRoot, "apps");
        File app = new File(appDir, "com.sdu.doo.gsui.apk");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("noReset", true);//不需要每次安装app
        capabilities.setCapability("deviceName", "85GBBMD22AJY");
        capabilities.setCapability("automationName", "Appium");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("platformVersion", "5.1");
        capabilities.setCapability("autoGrantPermissions","ture");//允许在手机上安装该app
        capabilities.setCapability("appPackage", "com.sdu.doo.gsui");
        capabilities.setCapability("appActivity", "com.doo.driver.sdk.LauncherActivity");
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), (Capabilities) capabilities);
    }
    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void login () throws InterruptedException {
//        clearText(By.xpath());
        driver.findElement(By.xpath("//android.widget.EditText[@resource-id='com.sdu.didi.gsui:id/et_phone' and @text = '请输入手机号码']")).click();
        driver.pressKeyCode(8);driver.pressKeyCode(8);driver.pressKeyCode(7);driver.pressKeyCode(7);driver.pressKeyCode(7);
        driver.pressKeyCode(7);driver.pressKeyCode(7);driver.pressKeyCode(7);driver.pressKeyCode(13);driver.pressKeyCode(15);
        driver.pressKeyCode(8);
        Thread.sleep(1000);
//        WebElement element = (WebElement) By.xpath("//android.widget.CheckBox[@resource-id='com.sdu.didi.gsui:id/cb_law' and @checked = 'false']");
//        WebElement element1 = (WebElement) By.id("com.sdu.didi.gsui:id/cb_law");
        WebElement element = driver.findElement(By.xpath("//android.widget.CheckBox[@text = '同意']"));
        System.out.println(element.getAttribute("checked"));
        if (element.getAttribute("checked") == "false"){
            driver.findElement(By.id("com.sdu.didi.gsui:id/cb_law") ).click();//同意
            }
        driver.findElement(By.id("com.sdu.didi.gsui:id/btn_next")).click();//下一步
    }
Copy after login

The methods to obtain the element status in sequence are:

element.getAttribute("checked")element.getAttribute("text")
element.getAttribute("resource-id")
element.getAttribute("name")  name是获取content-desc的值
element.size()
element.location()
Copy after login

And so on...

Special note: as long as the attribute value in the element is Boolean, that is, false or true. They can all be obtained through this method, so I won’t list them one by one. The second method here is often used, so focus on mastering it! !

The above is the entire content of this article. For more exciting information about Java, you can pay attention to the Java Video Tutorial and Java Development Tutorial columns on the PHP Chinese website! ! !

The above is the detailed content of Introduction to methods of obtaining element status in Appium and Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template