【问题标题】:How can I use @BeforeTest for Capabilities class / or correctly extends on each @Test如何将@BeforeTest 用于 Capabilities 类/或在每个 @Test 上正确扩展
【发布时间】:2019-05-15 13:18:32
【问题描述】:

我想创建一个类来插入@BeforeTest Capabilities。参考下面的代码,很容易将测试插入@Test。 没有TestNG 一切正常,但它不适用于TestNG。 也许我误解了什么?

能力类

public class test {

        public static AndroidDriver<AndroidElement> Capabilities() throws MalformedURLException {

            File f =new File("src");
            File fs = new File(f,"ApiDemos-debug.apk");
            DesiredCapabilities capabilities = new DesiredCapabilities();

            capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "demo");
            capabilities.setCapability(MobileCapabilityType.APP, fs.getAbsolutePath());
            AndroidDriver<AndroidElement> driver= new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);

             return driver;
        }

从测试扩展能力的测试示例

public class swiping extends test {

    public static void main(String[] args) throws MalformedURLException, InterruptedException {
        AndroidDriver<AndroidElement> driver=Capabilities();
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);        
        driver.findElementByXPath("//android.widget.TextView[@text='Views']").click();

        driver.findElementByXPath("//android.widget.TextView[@text='Date Widgets']").click();

        driver.findElementByAndroidUIAutomator("text(\"2. Inline\")").click();

        driver.findElementByXPath("//*[@content-desc='9']").click();

         Thread.sleep(1000);

        TouchAction t=new TouchAction(driver);

        WebElement first=driver.findElementByXPath("//*[@content-desc='15']");
        WebElement second=driver.findElementByXPath("//*[@content-desc='45']");
        t.longPress(longPressOptions().withElement(element(first)).withDuration(ofSeconds(2))).moveTo(element(second)).release().perform();
    }

}

【问题讨论】:

  • 我相信这是您在没有 Testng 的情况下使用的方法。您能否分享一下您使用 Testng 尝试过的方法?
  • 是的,这个例子没有TestNg,因为我不知道如何正确地将具有Capabilities的类引入@Beforetest

标签: java selenium testng appium


【解决方案1】:

下面的例子展示了一个如何做同样事情的 TestNG 例子。

该示例利用了ThreadLocal,因此明天如果您决定并行运行多个@Test 方法,其中每个@Test 方法都需要自己的AndroidDriver 实例,这仍然有效。

import static io.appium.java_client.touch.LongPressOptions.longPressOptions;
import static io.appium.java_client.touch.offset.ElementOption.element;
import static java.time.Duration.ofSeconds;

import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestClassExample {

  //We use thread local so that even if you decide to run tests in parallel, every @Test method
  //will get its own AndroidDriver instance
  private static final ThreadLocal<AndroidDriver> drivers = new ThreadLocal<>();

  @BeforeMethod
  public void setupDriver() throws MalformedURLException {
    File f = new File("src");
    File fs = new File(f, "ApiDemos-debug.apk");
    DesiredCapabilities capabilities = new DesiredCapabilities();

    capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "demo");
    capabilities.setCapability(MobileCapabilityType.APP, fs.getAbsolutePath());
    AndroidDriver driver =
        new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    drivers.set(driver);
  }

  @AfterMethod
  public void cleanupDriver() {
    drivers.get().quit();
    drivers.remove();
  }

  @Test
  public void testMethod() throws InterruptedException {
    AndroidDriver driver = drivers.get();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.findElementByXPath("//android.widget.TextView[@text='Views']").click();

    driver.findElementByXPath("//android.widget.TextView[@text='Date Widgets']").click();

    driver.findElementByAndroidUIAutomator("text(\"2. Inline\")").click();

    driver.findElementByXPath("//*[@content-desc='9']").click();

    Thread.sleep(1000);

    TouchAction t = new TouchAction(driver);

    WebElement first = driver.findElementByXPath("//*[@content-desc='15']");
    WebElement second = driver.findElementByXPath("//*[@content-desc='45']");
    t.longPress(longPressOptions().withElement(element(first)).withDuration(ofSeconds(2)))
        .moveTo(element(second))
        .release()
        .perform();
  }
}

PS:您不应该使用@BeforeTest,因为每个&lt;test&gt; 标签只会执行一次。你最好使用@BeforeClass(每个测试类执行一次(或)@BeforeMethod(每个@Test方法执行一次)

【讨论】:

  • 哇,真的很酷,谢谢,但我遇到了另一个问题(也许你可以帮我解决这个问题?失败的配置:@BeforeMethod setupDriver org.openqa.selenium.WebDriverException:不可能创建一个新的会话,因为未找到需要 HttpClient、InputStream 和 long 的“createSession”或无法访问构建信息:版本:“3.14.0”,修订:“aacccce0”,时间:“2018-08-02T20:19:58.91Z ' 我的 pom 文件 pastebin.com/0QHtyHKv appium logs pastebin.com/GETq7S5c
  • 您能否将其作为一个新问题发布,因为我认为当前问题与您遇到的这个新问题无关。
猜你喜欢
  • 1970-01-01
  • 2017-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-17
  • 1970-01-01
  • 2022-01-21
相关资源
最近更新 更多