有困难找到使用XPath和CSS中硒的Andr​​oid webdriver的测试要素 [英] Having difficulty in finding Elements using Xpath and CSS in Selenium Android Webdriver Testing

查看:246
本文介绍了有困难找到使用XPath和CSS中硒的Andr​​oid webdriver的测试要素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动使用Selenium webdriver的Andr​​oid的Web应用程序,我已经运行一个简单的程序打开谷歌页面和搜索的术语。虽然寻找的名字和ID的元素,它完美运行。不过,虽然试图找到与XPath和CSS中的元素似乎很难。是否有人知道如何使用XPath和CSS路径硒的Andr​​oid webdriver的?下面是示例code我用:

 公共类TestDriver扩展的TestCase
{

    公共静态无效testGoogle()抛出异常
    {
    AndroidDriver驱动程序=新AndroidDriver();


    driver.get(http://google.com);
    WebElement元= driver.findElement(By.name(Q));
       // WebElement元= driver.findElement(By.xpath(// * [@ ID ='gbqfq']));
       // WebElement元= driver.findElement(By.cssSelector(#gbqfq));
    element.sendKeys(机器人驾驶);
    element.submit();
    的System.out.println(页面的标题是:+ driver.getTitle());
    //driver.quit();
  }

}
 

解决方案

我已成功地证明了 By.xpath By.cssSelector 能找到的元素,然后可以使用我们的code与网页互动。一对夫妇的实际原因,我创建了一个影子,简化测试页,然后提交查询,从谷歌的搜索引擎得到的搜索结果。

下面是简化的测试页,足以证明的基础知识。将此文件复制到你的Andr​​oid设备或AVD如的SD卡使用下面的命令亚行推form.html /sdcard/form.html

 < HTML>
< HEAD>
  <冠军> webdriver的形式测试页< /标题>
< /头>
<身体GT;
<表格名称=TESTFORM行动=htt​​p://www.google.co.uk/search的方法=获得>
  <输入ID =gbqfq级=gbqfif类型=文字值=自动完成=关闭NAME =Q>
  <输入类型=提交值=谷歌搜索>
< /形式GT;
< /身体GT;
< / HTML>
 

和这里的code在你的问题修改后的版本:

  @Test
公共无效testByClauses()抛出异常{
     AndroidDriver驱动程序=新AndroidDriver();
     // driver.get(http://www.google.co.uk/);
     driver.get(文件:///sdcard/form.html);
     的System.out.println(driver.getPageSource());

     WebElement nameElement = driver.findElement(By.name(Q));
     WebElement xpathElement = driver.findElement(By.xpath(// * [@ ID ='gbqfq']));
     WebElement cssElement = driver.findElement(By.cssSelector(#gbqfq));

     nameElement.sendKeys(机器人);
     xpathElement.sendKeys(驱动程序);
     cssElement.sendKeys(webdriver的);
     nameElement.submit();
     的System.out.println(页面的标题是:+ driver.getTitle());
     driver.quit();
}
 

我碰巧使用JUnit 4作为测试运行(因此@Test标注)然而,这code也应该在JUnit 3中。

注意要点:

  • 在我特意用这指的是同一个底层的HTML元素3个独立的WebElements
  • 在每个的SendKeys(...)通话追加文本搜索查询
  • 在技术上我们并不需要提交的搜索查询展示通过(...)条款的工作,但是这是很好的看到code执行并得到一些匹配的搜索结果。
  • 在我注释掉加载谷歌搜索主页上的要求,但离开它在我的答案,所以你可以很容易地看到如何使用,而不是静态的本地HTML页面中的谷歌网站。
  • 我跑测试与本地连接的Nexus 4手机,通过USB和使用本地建实例 AndroidDriver()

I want to automate android web apps using Selenium WebDriver and i've run a simple program to open the google page and search a term . While finding the element with the name and id it runs perfectly. But while trying to find the element with the Xpath and Css seems difficult. Does anybody know how to use xpath and css path in selenium android webdriver? Here is the sample code i've used:

public class TestDriver extends TestCase 
{

    public static void testGoogle() throws Exception 
    {
    AndroidDriver driver = new AndroidDriver();


    driver.get("http://google.com");
    WebElement element = driver.findElement(By.name("q"));
       //WebElement element = driver.findElement(By.xpath("//*[@id='gbqfq']"));
       //WebElement element = driver.findElement(By.cssSelector("#gbqfq"));
    element.sendKeys("android driver");
    element.submit();
    System.out.println("Page title is: " + driver.getTitle());
    //driver.quit();
  }

}

解决方案

I have managed to prove that By.xpath and By.cssSelector are able to find elements which can then use in our code to interact with the web page. For a couple of practical reasons I created a shadow, simplified test page which then submits a query to obtain search results from Google's search engine.

Here is the simplified test page, enough to demonstrate the basics. Copy this file to the sdcard of your android device or AVD e.g. using the following command adb push form.html /sdcard/form.html

<html>
<head>
  <title>WebDriver form test page</title>
</head>
<body>
<form name="testform" action="http://www.google.co.uk/search" method="get">
  <input id="gbqfq" class="gbqfif" type="text" value="" autocomplete="off" name="q">
  <input type="submit" value="search Google">
</form>
</body>
</html>

And here's the modified version of the code in your question:

@Test
public void testByClauses() throws Exception {
     AndroidDriver driver = new AndroidDriver();
     // driver.get("http://www.google.co.uk/");
     driver.get("file:///sdcard/form.html");
     System.out.println(driver.getPageSource());

     WebElement nameElement = driver.findElement(By.name("q"));
     WebElement xpathElement = driver.findElement(By.xpath("//*[@id='gbqfq']"));
     WebElement cssElement = driver.findElement(By.cssSelector("#gbqfq"));

     nameElement.sendKeys("android ");
     xpathElement.sendKeys("driver ");
     cssElement.sendKeys("webdriver");
     nameElement.submit();
     System.out.println("Page title is: " + driver.getTitle());
     driver.quit();
}

I happen to be using JUnit 4 as the test runner (hence the @Test annotation) however this code should also work in JUnit 3.

Points to note:

  • I have deliberately used 3 separate WebElements which refer to the same underlying HTML element
  • Each sendKeys(...) call appends text to the search query
  • Technically we don't need to submit the search query to demonstrate the By(...) clauses work, however it's good to see the code execute and get some matching search results.
  • I commented out the request to load Google search's homepage, but left it in my answer so you can easily see how to use the Google site instead of the static local HTML page.
  • I ran the test with a locally connected Nexus 4 phone, over USB, and used a locally built instance of the AndroidDriver().

这篇关于有困难找到使用XPath和CSS中硒的Andr​​oid webdriver的测试要素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆