并行完成多个类的testNg执行中的异常行为:SeleniumWebdriver + TestNG [英] Erratic behaviour in testNg execution of multiple classes done in parallel: SeleniumWebdriver +TestNG

查看:138
本文介绍了并行完成多个类的testNg执行中的异常行为:SeleniumWebdriver + TestNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看testng.xml并提出建议
我想运行2个类(如下所述),但由于在下面提到的2个方案下显示出一些不稳定的行为,因此面临一些挑战.

Please have a look in the testng.xml and please suggest
I want to run 2 classes (mentioned below) but facing some challenges as it shows some erratic behaviours under 2 scenarious as mentioned below.

设置和说明:

SetUp & Description:

  1. 共有2个测试类:1. HomepageTest& SignIntest

  1. There are 2 test Classes: 1. HomepageTest & SignIntest

这些测试类下的方法具有组和优先级
**目标是使用

Methods under these test classes have Groups and Priorities
**Target is to run these test using groups

场景1::当我使用Threadcount = 1时;
[test name ="AutoPracticee" parallel ="classes" thread-count ="1"]

以下是行为:

Scenario1: When I use Threadcount =1;
[test name="AutoPracticee" parallel = "classes" thread-count = "1"]

below are the behaviours:

1.1.执行时,它将启动Homepage [在window1上]>执行Hometest的[Pri​​ority1]>
,然后启动SignInPage [在Window2 (此处也打开了窗口1)] >>并执行以下项的[priority1]: SignIntest
== >>此后,它执行HomepageTest的[priority2]和SigninTest的
[priority2]等. strong>
[请参阅问题1]

1.1. On execution it launches Homepage[On window1] > executes [Priority1] of Homepagetest >
Then launches SignInPage [On Window2 (here window 1 is also open)] >> and executes [priority1] of SignIntest
==>>After this it executes [priority2] of HomepageTest and the
[priority2] of SigninTest and so on..
[i.e execution toggles between Homepage and SignInpage on priority basis]
[Refer Question1]

1.2.执行完成后,它将关闭1个窗口(主页)
,但无法关闭另一个浏览器(SigninPage)
[此处所有打开的窗口都应关闭,但1个关闭,1个保持打开状态] [请参阅问题2]

****方案2:**当我使用threadcount = 2时:
**
行为:
2.1.它在chrome上同时打开2个窗口-在一个窗口中启动URL(URL等待10秒钟)
但是在第二个窗口中,它不启动任何URL(在整个测试过程中保持空白)

1.2. Once Execution finishes, it closes 1 window (Homepage)
but fails to close another browser(SigninPage)
[here all opened windows should get closed but 1 closes and 1 remains open] [Refer Question2]

****Scenario2:** When I use threadcount = 2:
**
Behaviours:
2.1. It opens 2 window simultaneously on chrome -- In one window it launches URL(For URL wait for 10 seconds)
but in 2nd window it doesnt launch any URL (Remains blank throughout test process)

2.2:执行与上述1.1相同.
所有执行都在一个窗口上进行,
另一个窗口(空白窗口)在测试过程中始终处于空闲状态.甚至没有为URL分配

2.2: Execution takes place just like 1.1 mentioned above.
All execution takes place on 1 window,
the other window(Blank window) remains in idle state throughout test process..not even URL gets assigned to it

2.3执行完成后,它仅关闭正在执行执行操作的1个窗口.
也就是说,它无法关闭没有启动URL的窗口

2.3 After execution finishes it closes only 1 window on which the execution was taking place
i.e. it fails to close the window on which no URL is launched

问题1:[针对两种情况] 为什么执行不是基于组的.
它在1.1中提到的优先级基础上运行,这看起来是不正确的.

Question1: [For Both Scenarios] Why Execution is not on the basis of Group.
It is running on the basis of priority as mentioned in 1.1 which looks incorrect.

理想情况下,它应该执行testClass1的Group1& 2. 然后应该关闭该窗口,然后启动第二个窗口并运行Group1& 2测试,然后关闭该窗口

Ideally it should execute Group1&2 of testClass1
then it should close that window Then should launch 2nd window and run Group1&2 tests and then close that window

问题2:[针对两种情况] 为什么无法关闭1个浏览器.我使用
窗口句柄计算了活动窗口,计数返回1,而应为2,因为webdriver打开了2个窗口

Question2: [for Both Scenarios] Why It fails to close the 1 Browser. I counted the active window using
window handle and count was returning 1 whereas it should be 2 as webdriver has opened 2 windows

问题3:[针对第二种情况] :当线程数= 2时,为什么它启动空白窗口
(如2.1中所述)

Question3: [for 2nd Scenario] When thread-count =2 then why it is launching a blank window
(As mentioned in 2.1)

//testNG.xml

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="AutoPracticee" > <!--   -->
        <parameter name="Browser" value="CH"></parameter>
        <parameter name="URL" 
value="http://automationpractice.com/index.php"></parameter>
        <!-- <parameter name="OSName" value="Windows"></parameter>-->       
<test name="AutoPracticee" parallel = "classes" thread-count = "1"> 
            <!-- For scenario2, execute with -- thread-count = "2" -- -->
            <groups>
                <run>                       
                    <include name="smoke"/>
                    <include name="txt"/>

                <!-- OR 
                     <include name="Homepage_func"/>
                    <include name="SignInpage_func"/>
                Try this set of group also(Both together as a set)
                -->


                </run>
            </groups>
            <classes>
                 <class name= 

"com.automationpracticee.framework.test.createaccountandlogin.SignInTest"/>

                 <class name= 
"com.automationpracticee.framework.test.homepage.HomepageTest"/>   


            </classes>
        </test>    
    </suite>

//HomepageTest:

// HomepageTest:

public class HomepageTest extends UI_GenericMethods{
    Homepage homeObj;
    SignInPage SignInPageObj;

    @BeforeClass(alwaysRun = true)`enter code here`
    @Parameters({ "Browser", "URL" })
    public void getBrowser(String Browser, String URL) throws IOException, 
     InterruptedException {
        System.out.println("getBrowser Method ");
        System.out.println("Launch " + URL + " on " + Browser);
        try {
            homeObj = gm_OpenApp(Browser, URL);

        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    @AfterClass(alwaysRun = true)
    public void tearDown_HP() throws InterruptedException, IOException {
        System.out.println("In tear Down method_HP");
        System.out.println("closing now_HP");
        driverObj.quit();
    }

    // "********Validation of SignIn Link********");
    @Test(priority = 0, groups = { "Homepage_func", "smoke"})
    public void validateHeaderSignInLink_HP() throws Exception {

        System.out.println("In validateHeaderSignInLink Method_HP");
        System.out.println("Out  of validateHeaderSignInLink method_HP");
    }

    // "********Validation of GetSavingNow Button********");
    @Test(priority = 1, groups = { "Homepage_func", "smoke" })
    public void validateGetSavingNowButton_HP() throws Exception {

        System.out.println("In validateGetSavingNowButton Method_HP");
        System.out.println("Out  of validateGetSavingNowButton method_HP");
    }

    @Test(priority = 2, groups = { "Homepage_func", "smoke"})
    // "********Validation of SearchBox********");
    public void validateSearchField_HP() throws Exception {
        System.out.println("In validateSearchField Method_HP");
        System.out.println("Out  of validateSearchField method_HP");
    }

    @Test(priority = 3, groups = { "Homepage_func", "smoke" })
    // "********Validation of Logo********");
    public void validateLogoIcon_HP() throws Exception {
        System.out.println("In validateLogoIcon Method_HP");
        System.out.println("Out  of validateLogoIcon method_HP");
    }

    @Test(priority = 4, groups = { "Homepage_func", "smoke"})
    // "********Validation of Slider1********");
    public void validateHomepageSlider1_HP() throws Exception {
        System.out.println("In validateHomepageSlider1 Method_HP");
        System.out.println("Out  of validateHomepageSlider1 method_HP");

    }

    @Test(priority = 5, groups = { "Homepage_func", "func"})
    // "********Validation of Slider2********");
    public void validateHomepageSlider2_HP() throws Exception {
        System.out.println("In validateHomepageSlider2 Method_HP");
        System.out.println("Out  of Validation of Slider2 method_HP");
    }

    @Test(priority = 6, enabled = false, groups = {"Homepage_func", "func"})
    // "********Validation of Slider3********");
    public void validateHomepageSlider3_HP() throws Exception {
        System.out.println("In validateHomepageSlider3 Method_HP");
        System.out.println("Out  of Validation of Slider3 method_HP");
    }

    // "********Validation of CarouselBlocks********");
    @Test(priority = 7, groups = { "Homepage_func", "func"})
    public void validateHomepageCarouselBlocks_HP() throws Exception {
        System.out.println("In validateHomepageCarouselBlocks Method_HP");
        System.out.println("Out of validateHomepageCarouselBlocks 
     Method_HP");
    }

    // "********Validation of TopPromoBlocks********");
    @Test(priority = 8, groups = { "Homepage_func", "func"})
    public void validateHomepageTopPromoBlocks_HP() throws Exception {
        System.out.println("In validateHomepageTopPromoBlocks Method_HP");
        System.out.println("Out of validateHomepageTopPromoBlocks 
      Method_HP");

    }


    @Test(priority = 9, groups = { "Homepage_txt", "txt"})
    public void validateproductBlock_productName_HP() throws Exception {
        System.out.println("In validateproductBlock_productName_HP Method_HP 
        ");
        System.out.println("Out of validateproductBlock_productName_HP 
         Method_HP ");

    }

    @Test(priority = 10, groups = { "Homepage_txt", "txt"})
    public void validateproductBlock_FooterLinkName_HP() throws Exception {
        System.out.println("In validateproductBlock_FooterLinkName_HP 
        Method_HP ");
        System.out.println("Out of validateproductBlock_FooterLinkName_HP 
        Method_HP ");

     }
 }

//SignInTest:

//SignInTest:

 public class SignInTest extends UI_GenericMethods{
    Homepage homeObj;
    SignInPage lognObj; 
    @BeforeClass (alwaysRun = true)
    @Parameters({ "Browser", "URL" })
    public void setup_SIP(String Browser, String URL) throwsIOException{
        System.out.println("setup_SIP");
        System.out.println("Launch "+URL+" on "+Browser);
        try {
                homeObj = gm_OpenApp(Browser, URL);
                System.out.println("*Now navigate to login  page*");                                             
                lognObj = homeObj.navigateToSignInPage();
                parentWindow = driverObj.getWindowHandle();
                System.out.println("***Login page is in display***");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    @AfterClass (alwaysRun = true)
    public void tearDown_SIP() throws InterruptedException, IOException{
        System.out.println("In tear Down method_SIP ");
        System.out.println("closing now_SIPP");
        driverObj.quit();       
    }     
    @Test (priority = 1, groups = {"SignInpage_txt", "txt"})
    public  void validateSignInPage_PageHeading_SIP() throws Exception{
        System.out.println("In validateSignInPage_PageHeading Method_SIP");
        System.out.println("Out  of validateSignInPageHeading method_SIP");
    }   
        @Test (priority = 2, groups = {"SignInpage_txt", "txt"})
        public  void validateSignInPage_SignInSectionHeading_SIP() throws 
         Exception{
         System.out.println("In validateSignInPage_SignInSectionHeading 
         Method_SIP");
        System.out.println("Out  of 
    validateSignInPage_SignInSectionHeading method_SIP");
        }   
   @Test (priority = 3, groups = {"SignInpage_txt", "txt"})
   public  void validateSignInPageBreadcrumbText_SIP() throws Exception{
            System.out.println("In validateSignInPageBreadcrumbText 
            Method_SIP");
            System.out.println("Out  of validateSignInPageBreadcrumbText 
             method_SIP");
        }   
         @Test (priority = 4, groups = {"SignInpage_txt", "txt"})
        public  void validateRegistrationSectionHeading_SignInPage_SIP() 
        throws Exception{
            System.out.println("In 
            validateRegistrationSectionHeading_SignInPage Method_SIP");
            System.out.println("Out  of 
            validateRegistrationSectionHeading_SignInPage method_SIP");
        }   
        @Test (priority = 5, groups = {"SignInpage_func", "smoke"})
        public void validateLoginProcess_SIP() throws Exception {
            System.out.println("In validateLoginProcess Method_SIP");
            System.out.println("Out  of validateLoginProcess method_SIP");
        }           
        @Test (priority = 6, groups = {"SignInpage_func", "func"})
        public void validateForgotPasswordLink_SIP() throws IOException, 
        InterruptedException, InvalidFormatException{
            System.out.println("In validateForgotPasswordLink Method_SIP");
            System.out.println("Out  of validateForgotPasswordLink 
           method_SIP");        
        }
    } 

可重用方法库

public class UI_GenericMethods{

    protected static WebDriver driverObj;
    protected Homepage homeObj;


    /****************************************
     * Launch Browser & URL
     ****************************************************/

    /******************** Launch Browser **************************/

    public void gm_LaunchBrowser(String browserName){

        if (browserName.equalsIgnoreCase("CH") == true) {
        System.getProperty("webdriver.chrome.driver", "Please put Path 
         here");
        driverObj = new ChromeDriver();

        // Launch Firefox browser
    } else if (browserName.equalsIgnoreCase("FF") == true) {
        System.getProperty("webdriver.gecko.driver", "Please put Path 
        here");
        driverObj = new FirefoxDriver();        
    } else {
        System.out.println("Please enter correct browser name");
    }   
       driverObj.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
       driverObj.manage().timeouts().pageLoadTimeout(200, TimeUnit.SECONDS);
       driverObj.manage().window().maximize();

  }

    /******************** Open Site **************************/
    public static void gm_OpenURL(String URL) {
        driverObj.get(URL);
    }

   public Homepage gm_OpenApp(String BrowserName, String URL){
       System.out.println("In gm_OpenAp Method");
       gm_LaunchBrowser(BrowserName);
       try { //Just to wait for few seconds before and after URL launch
           Thread.sleep(10000);
       } catch (InterruptedException e) {       
          e.printStackTrace();
       }
       gm_OpenURL(URL);  //URL Launched
       try {
          Thread.sleep(10000);
       } catch (InterruptedException e) {       
            e.printStackTrace();
       }
        System.out.println("URL done");
       System.out.println("Now Homepage is in display");
       homeObj = PageFactory.initElements(driverObj, Homepage.class);
       return homeObj;
     }
  }

场景2的控制台输出
setup_SIP
getBrowser方法
在CH上启动 http://automationpractice.com/index.php
在CH上启动 http://automationpractice.com/index.php
在gm_OpenAp方法中
在gm_OpenAp方法中
CH- http://automationpractice.com/index.php
CH- http://automationpractice.com/index.php
1 G:\ QA \ AutomationTools \ WorkspaceMars1 \ 1.2hp.com.automationprac \ ChromeDriver \ Chromedriver_win32_v2.38 \ chromedriver.exe
1 G:\ QA \ AutomationTools \ WorkspaceMars1 \ 1.2hp.com.automationprac \ ChromeDriver \ Chromedriver_win32_v2.38 \ chromedriver.exe *
2 G:\ QA \ AutomationTools \ WorkspaceMars1 \ 1.2hp.com.automationprac \ ChromeDriver \ Chromedriver_win32_v2.38 \ chromedriver.exe
2G:\ QA \ AutomationTools \ WorkspaceMars1 \ 1.2hp.com.automationprac \ ChromeDriver \ Chromedriver_win32_v2.38 \ chromedriver.exe
启动ChromeDriver 2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb)
端口17592
仅允许本地连接.
启动ChromeDriver 2.38.552522(437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb) 在端口11565上
仅允许本地连接.
2018年9月2日上午3:38:46 org.openqa.selenium.remote.ProtocolHandshake createSession
信息:检测到的方言:OSS
2018年9月2日上午3:38:46 org.openqa.selenium.remote.ProtocolHandshake createSession
信息:检测到的方言:OSS
浏览器名称已完成
浏览器名称已完成
URL已完成 [我的评论-此处为空白[用于方案2]]
现在显示主页
*******现在导航到登录页面*******
在validateHeaderSignInLink方法_HP中
超出validateHeaderSignInLink方法_HP
URL已完成
现在显示主页
*******现在导航到登录页面*******
1
2
*******显示登录页面*******
在validateSignInPage_PageHeading方法_SIP中
超出validateSignInPageHeading方法_SIP
在validateSignInPage_SignInSectionHeading方法_SIP中
在validateGetSavingNowButton方法中_HP
超出validateSignInPage_SignInSectionHeading方法_SIP
超出validateGetSavingNowButton方法_HP
在validateSignInPageBreadcrumbText方法中_SIP
超出validateSignInPageBreadcrumbText方法_SIP
在validateSearchField Method_HP中
超出validateSearchField方法_HP
在validateRegistrationSectionHeading_SignInPage方法_SIP中
超出validateRegistrationSectionHeading_SignInPage方法_SIP

在validateLogoIcon方法中_HP

out of validateLogoIcon方法_惠普
在validateLoginProcess方法中_SIP
超出validateLoginProcess方法_SIP
在拆解方法中_SIP
关闭now_SIPP ** [我的评论-无法关闭[针对方案1& 2]

在validateHomepageSlider1方法中_HP
超出validateHomepageSlider1方法_HP
在validateproductBlock_productName_HP方法_HP中
超出validateproductBlock_productName_HP Method_HP
在validateproductBlock_FooterLinkName_HP方法_HP中
超出validateproductBlock_FooterLinkName_HP Method_HP
拆解方法_HP
现在关闭_HP [我的评论-此处已关闭[针对方案1和2]

[INFO]测试运行:12,失败:0,错误:0,跳过:0,经过的时间:
44.759 s-在TestSuite中
[INFO]
[INFO]结果:
[INFO]
[INFO]测试运行:12,失败:0,错误:0,跳过:0
[INFO]
[INFO] -------------------------------------------- ---------------------
[INFO]成功建立
[INFO] -------------------------------------------- ------------------
[INFO]总时间:50.490 s

Console Output for Scenario2
setup_SIP
getBrowser Method
Launch http://automationpractice.com/index.php on CH
Launch http://automationpractice.com/index.php on CH
In gm_OpenAp Method
In gm_OpenAp Method
CH -- http://automationpractice.com/index.php
CH -- http://automationpractice.com/index.php
1 G:\QA\AutomationTools\WorkspaceMars1\1.2hp.com.automationprac\ ChromeDriver\Chromedriver_win32_v2.38\chromedriver.exe
1 G:\QA\AutomationTools\WorkspaceMars1\1.2hp.com.automationprac\ ChromeDriver\Chromedriver_win32_v2.38\chromedriver.exe*
2 G:\QA\AutomationTools\WorkspaceMars1\1.2hp.com.automationprac\ ChromeDriver\Chromedriver_win32_v2.38\chromedriver.exe
2G:\QA\AutomationTools\WorkspaceMars1\1.2hp.com.automationprac\ ChromeDriver\Chromedriver_win32_v2.38\chromedriver.exe
Starting ChromeDriver 2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb)
port 17592
Only local connections are allowed.
Starting ChromeDriver 2.38.552522(437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb) on port 11565
Only local connections are allowed.
Sep 02, 2018 3:38:46 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Sep 02, 2018 3:38:46 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Browsrname done
Browsrname done
URL done [MY COMMENT -- This remains blank[for Scenario2]]
Now Homepage is in display
*******Now navigate to login page*******
In validateHeaderSignInLink Method_HP
Out of validateHeaderSignInLink method_HP
URL done
Now Homepage is in display
*******Now navigate to login page*******
1
2
*******Login page is in display*******
In validateSignInPage_PageHeading Method_SIP
Out of validateSignInPageHeading method_SIP
In validateSignInPage_SignInSectionHeading Method_SIP
In validateGetSavingNowButton Method_HP
Out of validateSignInPage_SignInSectionHeading method_SIP
Out of validateGetSavingNowButton method_HP
In validateSignInPageBreadcrumbText Method_SIP
Out of validateSignInPageBreadcrumbText method_SIP
In validateSearchField Method_HP
Out of validateSearchField method_HP
In validateRegistrationSectionHeading_SignInPage Method_SIP
Out of validateRegistrationSectionHeading_SignInPage method_SIP

In validateLogoIcon Method_HP

Out of validateLogoIcon method_HP
In validateLoginProcess Method_SIP
Out of validateLoginProcess method_SIP
In tear Down method_SIP
closing now_SIPP **[My Comment -- fails to close [For scenario 1 & 2]

In validateHomepageSlider1 Method_HP
Out of validateHomepageSlider1 method_HP
In validateproductBlock_productName_HP Method_HP
Out of validateproductBlock_productName_HP Method_HP
In validateproductBlock_FooterLinkName_HP Method_HP
Out of validateproductBlock_FooterLinkName_HP Method_HP
In tear Down method_HP
closing now_HP [My Comment--This gets closed[For scenario1&2]

[INFO] Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed:
44.759 s - in TestSuite
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 12, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] -----------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] --------------------------------------------------------------
[INFO] Total time: 50.490 s

推荐答案

在决定使用并行执行时,请确保所使用的资源没有在多个线程之间共享.并确保它们是不可变的,以提高安全性. driverObj是静态的,并且所有测试类都可以同时访问它,这是问题的根本原因.您可以通过在测试方法内部创建和初始化驱动程序并将其安全传递来简化操作.另一种稍微复杂的方法是使用ThreadLocal

When deciding to use parallel execution, make sure that the resources you're using are not shared across multiple threads. And also make sure they're immutable for additional safety. The driverObj is static and is accessible by all your test classes at the same time and is the root cause of your problems. You can keep it simple by creating and initializing the driver inside the test method and pass it around safely. Another slightly complicated way would be use ThreadLocal

对于优先级,这是当前行为,即先运行高优先级测试,然后再运行低优先级测试和iirc,即使没有优先级,也不能保证执行顺序.

And for priority, that is how the current behavior is higher priority tests run first followed by the lowering priority ones and iirc even without priority, the execution order isn't guaranteed.

这篇关于并行完成多个类的testNg执行中的异常行为:SeleniumWebdriver + TestNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆