简体   繁体   English

使用appium和selenium网格运行自动化测试只能在一台设备上运行

[英]Running automate tests with appium and selenium grid only run in one device

I'm trying to have some mobile automated tests running with appium and selenium grid. 我正在尝试使用appium和selenium网格运行一些移动自动化测试。 Once i done all configuration stuff and added grid nodes, how do i run my tests parallely in both devices? 一旦我完成了所有配置并添加了网格节点,我如何在两个设备中并行运行我的测试?

Here's my setUp() : 这是我的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) 

What it supposed to be in deviceName in this case? 在这种情况下它应该在deviceName中是什么?

If i leave it blank, here's what i got: 如果我把它留空,这就是我得到的:

C:\Users\XXXXX\Desktop\workspace\XXXXX>java -jar selenium-server-standalone-2.44.0.jar -role hub

19:16:58.691 INFO - Launching a selenium grid server

2016-02-18 19:16:59.937:INFO:osjs.Server:jetty-7.x.y-SNAPSHOT

2016-02-18 19:16:59.968:INFO:osjsh.ContextHandler:startedo.s.j.s.ServletContextHandler{/,null}2016-02-18 19:16:59.995:INFO:osjs.AbstractConnector:StartedSocketConnector@0.0.0.0:4444

19:49:48.183 INFO - Got a request to create a new session: Capabilities[{app=C:\Users\XXXXX\Desktop\workspace\XXXXX\apps\XXXXX.apk, appPackage=XXXXXXX, appActivity=.MainActivity, noReset=true, platformVersion=5.1, platformName=Android, deviceName=}]

19:49:48.183 INFO - Available nodes: [host :http://127.0.0.1:4723, host :http://127.0.0.1:4733]

 19:49:48.183 INFO - Trying to create a new session on node host :http://127.0.0.1:4723

19:49:48.183 INFO - Trying to create a new session on test slot {newCommandTimeout=30, browserName=Android, maxInstances=1, version=5.1,deviceName=0429058934,deviceReadyTimeout=5, platform=ANDROID}

I only able to run the one registered node in grid. 我只能在网格中运行一个注册节点。 I even tried to create a script with two setup() , each one to each device, but even this way, tests only ran in this same device one device. 我甚至试图创建一个带有two setup()的脚本,每个设备对应一个设备,但即使这样,测试也仅在同一个设备中运行一个设备。

Here is my grid console: 这是我的网格控制台:

在此输入图像描述

    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 !!

From what I gather you are try to run your tests concurrently, right? 从我收集的内容中,您尝试同时运行测试,对吧?

If so, I haven't seen anything about threads in your post, and without threads your test will run serially. 如果是这样,我在帖子中没有看到任何关于线程的内容,如果没有线程,你的测试将连续运行。

The Selenium Grid doesn't round-robin connections for the same kind of resource. Selenium Grid不会为相同类型的资源进行循环连接。 It simply allocates the first available machine. 它只是分配第一台可用的机器。 IE if test 'A' requests a specific browser/platform/device config, and runs to completion, then if test 'B' comes along and asks for the same config, it'll get the same machine that test A got. IE如果测试'A'请求特定的浏览器/平台/设备配置,并运行完成,那么如果测试'B'出现并要求相同的配置,它将获得测试A得到的相同机器。 Make sense? 合理?

If you want to parallelize your tests, I would recommend checking out pytest & the xdist plugin. 如果你想并行测试,我建议你查看pytestxdist插件。 This will handle all the threading/multiprocess stuff for you. 这将为您处理所有线程/多进程的东西。

Fun fact, even if you wrote everything to use unittest, you don't have to rewrite everything to use pytest; 有趣的事实,即使你写了所有使用unittest的东西,你也不必重写所有东西来使用pytest; just point pytest at your existing code. 只需在现有代码中指向pytest即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何仅对一个设备指定appium - how to specify appium to one device only 使用python为Android设备运行appium测试时出现问题 - Problems running appium tests for an android device using python Appium / Protractor测试不能在真实设备上运行.APK - Appium/Protractor tests do not run on real device .APK 使用Appium运行Behat测试 - Running Behat tests with Appium 有没有办法让正在运行的移动设备成为焦点或使用 appium 在前台运行它 - Is there a way to bring a running mobile device into focus or run it in the foreground using appium 带有阿片和硒网格的并行测试 - parallel test with appium & selenium grid Appium节点样本测试未运行 - Appium node sample tests not running Appium测试不在AWS Device Farm上运行,但在本地运行良好 - Appium Tests Don't Run on AWS Device Farm but Run Fine Locally 如何使用Appium在真实设备上从jenkins安装* .apk并运行用python编写的测试? - How can I install *.apk from jenkins on real device with Appium and run tests written in python? 错误参数:BadParametersError:参数不正确。 我们希望在运行 Appium 时在 Selenium 网格上具有所需的功能 - Bad parameters: BadParametersError: Parameters were incorrect. We wanted required capabilities on Selenium grid while running Appium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM