使用appium和selenium网格运行自动测试只在一个devi中运行

2024-05-16 00:02:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用appium和selenium网格运行一些移动自动化测试。一旦我完成了所有的配置工作并添加了网格节点,如何在两个设备中并行运行测试?在

这是我的setUp()

desired_caps = {}
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '5.1'
    desired_caps['deviceName'] = ''
    desired_caps['app'] = os.path.abspath(os.path.join(os.path.dirname(__file__), 'C:/Users/XXXXX/Desktop/workspace/XXXX/apps/XXXXX.apk'))
    desired_caps['appPackage'] = 'XXXXXXXX'
    desired_caps['appActivity'] = '.MainActivity'
    desired_caps['noReset'] = False
    self.driver = webdriver.Remote('http://localhost:4444/wd/hub', desired_caps)
    self.driver.implicitly_wait(15) 

在这种情况下,deviceName中应该是什么?在

如果我留空,我得到的是:

^{pr2}$

我只能在网格中运行一个注册的节点。我甚至尝试用two setup()创建一个脚本,每个脚本都指向每个设备,但是即使这样,测试也只在同一个设备上运行一个设备。在

这是我的网格控制台:

enter image description here


Tags: pathself脚本网格节点osdriverselenium
2条回答

据我所知,你试图同时运行你的测试,对吗?在

如果是这样的话,我在你的帖子中没有看到关于线程的任何信息,如果没有线程,你的测试将连续运行。在

Selenium网格不会对同一种资源进行循环连接。它只是分配第一台可用的机器。一、 E.如果测试'A'请求一个特定的浏览器/平台/设备配置,并运行到完成,那么如果测试'B'出现并要求相同的配置,它将得到测试got的同一台机器。有道理?在

如果您想并行化测试,我建议您检查pytest&以及xdist插件。这将为您处理所有线程/多进程事务。在

有趣的是,即使您编写了使用unittest的所有内容,也不必重写所有内容来使用pytest;只需将pytest指向现有代码即可。在

    I did try to run tests using Grid with Appium server in java, same logic you can adopt for your own language. Here is the explanation:

    1. This is the same content of node.json file on two node machines:

    {
    "capabilities":
         [
           {
             "version":"4.4.2",
             "maxInstances": 3,
             "platformName":"ANDROID"
           }
         ],
    "configuration":
    {
       "cleanUpCycle":2000,
       "timeout":30000,
       "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
       "url":"http://WHERE_APPIUM_RUNNNING:4723/wd/hub",
       "host": "WHERE_APPIUM_RUNNNING_IP",
       "port": 4723,
       "maxSession": 6,
       "register": true,
       "registerCycle": 5000,
       "hubPort": 4444,
       "hubHost": "WHERE_HUB_RUNNNING_IP"
    }
    } 

    2. Downloaded selenium-server-standalone-2.52.0.jar and started as hub on one machine like: 
    java -jar ~/Downloads/selenium-server-standalone-2.52.0.jar -role hub maxInstances=2 maxSessions=2 

    3. Started ONLY appium server on node machines and registered with hub using command as: appium  nodeconfig ~/Desktop/node.json

    4. Now declare all the common capability in GridTest.java and only deviceName was passed from testNG.xml in order to avoid any hardcoding in code and keeping node.config as generic for all node machines, sample testNG.xml is like:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Automation" parallel="tests">

        <test name="Test1">
        <parameter name="deviceName" value="XYZZZZZ" />
            <classes>
                <class name="poc.grid.GridTest" />
            </classes>
        </test>

        <test name="Test2">
        <parameter name="deviceName" value="ZYXXXXX" />
            <classes>
                <class name="poc.grid.GridTest" />
            </classes>
        </test>

    </suite>

    5. And I wrote a GridTest.java class like:

    package poc.grid;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.concurrent.TimeUnit;

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.testng.annotations.Parameters;
    import org.testng.annotations.Test;


    public class GridTest {

        @Parameters({"deviceName"})
        @Test
        public void test (String deviceName) throws MalformedURLException, InterruptedException
        {
            appium_driver(deviceName);
        }

        public void appium_driver(String deviceName)
        {
            try
            {
                DesiredCapabilities capabilities = new DesiredCapabilities();
                capabilities.setCapability("deviceName", deviceName);
                capabilities.setCapability("platformName", "Android");

                if(deviceName.equalsIgnoreCase("4d0025b440ca90d5")){
                    capabilities.setCapability("app", "/XXXXXX/chocolate.apk");
                }else{
                    capabilities.setCapability("app", "/XXXXXX/chocolate.apk");
                }

                capabilities.setCapability("newCommandTimeout", "120");
                WebDriver driver = new RemoteWebDriver(new URL("http://WHERE_HUB_RUNNNING_IP:4444/wd/hub"), capabilities);

                driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            }
            catch(Exception e)
            {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
    }


6. If you are using eclipse, then Right Click on testNG.xml and select Run As  > TestNg Suite.

7. Now you can see I've kept a condition only for apk file location because the node machines have different directory, if possible you should choose a similar location which exists in all node machines, else you may have to live with if - else.  Hope This Helps !!

相关问题 更多 >