Erratic behaviour in testNg execution of multiple

2019-09-02 08:19发布

问题:

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. There are 2 test Classes: 1. HomepageTest & SignIntest

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

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

below are the behaviours:

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. 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: 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 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

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.

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

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

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:

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:

 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");        
        }
    } 

Reusable Methods Lib

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;
     }
  }

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

回答1:

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

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.