如何使用@BeforeTest for Capabilities类/或在每个@Test

时间:2018-12-14 09:29:41

标签: java selenium testng appium

我想创建一个在其中插入@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();
    }

}

1 个答案:

答案 0 :(得分:0)

下面的示例显示了如何执行同一操作的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,因为每个<test>标签只会执行一次。最好使用@BeforeClass(每个测试类执行一次)或@BeforeMethod(每个@Test方法执行一次)