将设备 udid 从测试参数传递到单独类中的 DesiredCapability

问题描述 投票:0回答:1

我有一个名为 CommonAndroidDriver 的单独类,其中包含功能和驱动程序。我希望能够为每个测试单独添加设备 udid 作为参数。

public class CommonAndroidDriver {

    public static AndroidDriver driver;
    private URL serverUrl;
    private DesiredCapabilities capabilities = new DesiredCapabilities();

    public CommonAndroidDriver() throws MalformedURLException {
        this.serverUrl = new URL("https:");
        capabilities.setCapability("appium:platformName", MobilePlatform.ANDROID);
        capabilities.setCapability("appium:automationName", "UiAutomator2");
        capabilities.setCapability("appium:appPackage", "blabla");
        capabilities.setCapability("appium:appActivity", "blabla");
        capabilities.setCapability("uftm:oauthClientId", "blabla");
        capabilities.setCapability("uftm:oauthClientSecret", "blabla");
        capabilities.setCapability("uftm:tenantId", "blabla");
        capabilities.setCapability("appium:udid", **here** context.getCurrentXmlTest().getParameter("udid") ?);
        capabilities.setCapability("appium:noReset", true);
        capabilities.setCapability("appium:autoGrantPermissions", true);

        this.driver = new AndroidDriver(serverUrl, capabilities);
    }
}

示例测试开始,我希望在 CommonAndroidDriver 中输入此 udid:

public class LoginTest extends BaseAppium {

    public LoginTest() throws IOException {
    }

    @Test
    @Parameters({"login", "password", "udid"})
    public void Login(String login, String password, String udid) throws Exception {

        //some test steps

    }
}

我开始将它与 ITestContext getCurrentXmlTest().getParameter 结合起来,但找不到将 udid 传递给功能的方法。

测试ngxml

<suite name="AndroidAppiumTest">
    <test name="LoginTests">
        <parameter name="loginmp" value="[email protected]"/>
        <parameter name="passwordtest" value="Test1234"/>
        <parameter name="udid" value="DeviceId"/>
        <classes>
            <class name="tests.LoginTest"/>
            <methods>
                <include name="Login"/>
            </methods>
        </classes>
    </test>
</suite>

BaseAppium

public class BaseAppium {

    CommonAndroidDriver cad = new CommonAndroidDriver();
    public AndroidDriver driver;


    public BaseAppium() throws IOException {
    }


    @BeforeClass
    public void setUp() throws IOException {
        this.driver = cad.driver;
    }

    @BeforeTest
    public void setTest() {
    }


    @AfterClass
    public void tearDown() throws IOException, InterruptedException {
        this.driver.terminateApp("app.name");
        this.driver.quit();
    }
}
java parameters testng appium desiredcapabilities
1个回答
0
投票

您的代码中有几个问题。

  • 测试不应该使用 AndroidDriver 的静态实例,因为这可能会干扰我们并行运行测试。
  • 我们正在
    CommonAndroidDriver
    的构造函数级别创建
    BaseAppium
    。我们应该通过
    setup()
    方法来创建它。

这是一个清理过的代码,显示了修复/更改的样子

import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.ITestContext;

import java.net.MalformedURLException;
import java.net.URL;

public class CommonAndroidDriver {
    // Intentionally using RemoteWebDriver to avoid my test project having to add dependency on appium client
    // just for illustration purposes.
    public RemoteWebDriver driver;
    private final URL serverUrl;

    public CommonAndroidDriver(ITestContext context) throws MalformedURLException {
        this.serverUrl = new URL("https:");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("appium:platformName", "Android");
        capabilities.setCapability("appium:automationName", "UiAutomator2");
        capabilities.setCapability("appium:appPackage", "blabla");
        capabilities.setCapability("appium:appActivity", "blabla");
        capabilities.setCapability("uftm:oauthClientId", "blabla");
        capabilities.setCapability("uftm:oauthClientSecret", "blabla");
        capabilities.setCapability("uftm:tenantId", "blabla");
        capabilities.setCapability("appium:udid", context.getCurrentXmlTest().getParameter("udid"));
        capabilities.setCapability("appium:noReset", true);
        capabilities.setCapability("appium:autoGrantPermissions", true);
        this.driver = new RemoteWebDriver(serverUrl, capabilities);
    }

    public RemoteWebDriver getDriver() {
        return this.driver;
    }
}
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.ITestContext;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

import java.io.IOException;

public class BaseAppium {

    public RemoteWebDriver driver;

    @BeforeClass
    public void setUp(ITestContext context) throws IOException {
        this.driver = new CommonAndroidDriver(context).getDriver();
    }

    @AfterClass
    public void tearDown() {
        this.driver.quit();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.