Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
1.1k views
in Selenium by Contributing Tester (14 points)
Hi ,

I am running 2 testcases on chrome and firefox
It start with 1 st testcase on chrome and  firefox parallely then once chrome completed
and close the browser mean while firefox session will go off and
start 2nd testcase on both chrome and firefox browser.

1st firefox browser wont run whole testcase execution

Can you suggest any solution ?

1 Answer

0 like 0 dislike
by The go-to Tester (181 points)
selected by
 
Best answer
Problem shows that your webdriver instance is being. It looks like either you are making your webdriver variable as static. Batter approach is to delcare and define webdriver instance inside @Test method.

 

Can you post your code which you're using to initiate webdriver object? It will help to provide detailed solution.
by
For example( i am doing  keyword driven framework)
 
1.class TestcaseScenario extends Event{

process of @Test -> not creating any object
just accessing driver from event class
}
2.class Event{
public WebDriver driver;

all method of actions like
public void openBrowser(){
driver= new fireforx();
}
public void closeBrowser(){
driver.close();
}
}
by
Some time even following problem facing

2 testcase running on chrome n firefox ,
process of both browser take and execute , but 1 browser(ex.firefox)  take twice execution of 1 testcase and another (chrome) takes only once.
by The go-to Tester (181 points)
your driver should be initialized inside @Test method and closed in there as well. It should be something like below.

@Test
public void myTest(){

WebDriver driver = event.openBrowser();
////your code
driver.close()

}
by
Hi,

After creating driver reference variable inside @Test , also Same problem facing .

One thing observed -
Before every testcase execution I have printed the " result folder statement".

create folder statement
1st  testcase - firefox

create folder statement
1st  testcase - chrome

execution of 1st testcase completed and close chrome  browser (1st testcase)

execution of 1st testcase  still processing on firefox( 1st testcase)

create folder statement
Start 2nd testcase on chrome

Wont display create folder statement
Start 2nd testcase on firefox(1st testcase process goes to idle state and start processing 2nd testcase)

Complete 2nd testcase on chrome(close the browser)

Complete 2nd testcase on firefox(close the browser)

create folder statment
Start  2nd testcase on firefox(as new browser)

complete process and close firefox browser(2nd testcase)

finally----
1st testcase (which is idle firefox browser) still available, it wont close
by The go-to Tester (181 points)
Can you please post your code? It will be of great help to answer your question.
by Contributing Tester (14 points)
public void openBrowser(String vBrowser, String vHubUrl) throws IOException, InterruptedException {
       
        DesiredCapabilities capability = null;
        switch (browsertype.valueOf(vBrowser)) {
        case IE:
           
            capability = DesiredCapabilities.internetExplorer();
            capability.setBrowserName("internet explorer");
            capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
            capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
            capability.setCapability(
                    InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
            capability.setJavascriptEnabled(true);
            break;

        case CHROME:
       
            capability = DesiredCapabilities.chrome();
            ChromeOptions options = new ChromeOptions();
            options.addArguments("test-type");
            options.addArguments("start-maximized");
            capability.setCapability(ChromeOptions.CAPABILITY, options);
            break;

        case FF:
            capability = DesiredCapabilities.firefox();
            break;

        case SAFARI:
            capability = DesiredCapabilities.safari();
            break;
        }
         
         driver = new RemoteWebDriver(new URL(vHubUrl), capability);
         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
       
    }
by The go-to Tester (181 points)
As I can see here, your driver instance is declared out of your method. You method should get an arguments as below.

public void openBrowser(String vBrowser, String vHubUrl, WebDriver driver) throws IOException, InterruptedException

Also make sure that your driver declaration is happening inside your @Test method and it should not be static.

Let me know if that helps you. Also drop your code here of your @Test method for us to assert.
by Contributing Tester (14 points)
I have method like

    public void enterSite(WebDriver driver) {
        driver.navigate().to(Driver.vURL);
    }

it will throw nullpointer execption
by Contributing Tester (14 points)
As above your discussion i have passed all parameter to other method, but it throw null pointer execption
by The go-to Tester (181 points)
did you try adding additional parameter to your method and per my understanding your code should look like.

   public void enterSite(WebDriver driver) {
         openBrowser("IE","http://localhost:4444/wd/hub",driver);
        driver.navigate().to(Driver.vURL);
    }

let me know.
by Contributing Tester (14 points)
@Test
public void testMethod(){
WebDriver driver= null;
//some code here
keyword()
}

public void keyword(){
switch(key){
case "openBrowser":
      openbrowser(browser,url,driver);
     break;
case "enterurl":
      enterUrl(driver);
     break;
}

here driver variable getting null

}
}
by Contributing Tester (14 points)
public void enterSite(WebDriver driver) {
        driver.navigate().to(Driver.vURL);
    }

this is method
by Contributing Tester (14 points)
are you require some more information ?
by Contributing Tester (14 points)
i am thinking  driver control getting off for processing testcaes
 when 2nd testcase start in firefox or chrome
by The go-to Tester (181 points)
Yes. The reason behind this is that, your driver instance is getting shared.
Are you using JUnit/TestNG?
by Contributing Tester (14 points)
TestNg  only we have configured
by The go-to Tester (181 points)
Your code look something like this.


@Test
public void testMethod(){
WebDriver driver= null;
//some code here
keyword(key,driver)
}

public void keyword(String key,WebDriver driver){
switch(key){
case "openBrowser":
      openbrowser(browser,url,driver);
     break;
case "enterurl":
      enterUrl(driver);
     break;
}
by Contributing Tester (14 points)
yes correct, driver variable getting null when enterurl called the method .
by The go-to Tester (181 points)
nice! hope it helped.
by Contributing Tester (14 points)
driver variable getting null, cant proceed to execution of process.
by The go-to Tester (181 points)
i think you should debug your code. What values you are passing for browser and URL?
by Contributing Tester (14 points)
hi,
i have debugged and solved , but same issue facing .

public WebDriver openBrowser(String vBrowser, String vHubUrl,WebDriver driver) throws IOException, InterruptedException {
        //browsertype browserType = browsertype.valueOf(vBrowser);
        //String userDir = System.getProperty("user.dir");
        //StringBuilder filename = new StringBuilder();
        DesiredCapabilities capability = null;
        switch (browsertype.valueOf(vBrowser)) {
        case IE:
            capability = DesiredCapabilities.internetExplorer();
            capability.setBrowserName("internet explorer");
            capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
            capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
            capability.setCapability(
                    InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
            capability.setJavascriptEnabled(true);
            break;

        case CHROME:

            capability = DesiredCapabilities.chrome();
            ChromeOptions options = new ChromeOptions();
            options.addArguments("test-type");
            options.addArguments("start-maximized");
            capability.setCapability(ChromeOptions.CAPABILITY, options);
            break;

        case FF:
            capability = DesiredCapabilities.firefox();
            break;

        case SAFARI:
            capability = DesiredCapabilities.safari();
            break;
        }
         
         driver = new RemoteWebDriver(new URL(vHubUrl), capability);
         System.out.println(driver);
         DriverSetUp setup= new DriverSetUp();
         setup.setDriver(driver);
         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
         driver.manage().window().maximize();
         return setup.getDriver();
       
    }


public class DriverSetUp {

   private WebDriver driver;

    public WebDriver getDriver() {
        return driver;
    }

    public void setDriver(WebDriver driver) {
        this.driver = driver;
    }
   
}


Keyword code

WebDriver driver1;
    public void executeKeywords(WebDriver driver) throws Exception
    {   
   
        switch(vKW){
        case "openBrowser":
            driver1=openBrowser(vBrowser, vHubUrl,driver);
            implicitWait();
            break;
        case "enterSite":
            enterSite(driver1);
            break;
        case "closeBrowser":
            driverQuit(driver1);
            break;
    }
by Contributing Tester (14 points)
Without creating getter n setter class(driversetup), just return webDriver from open browser , same issue facing .
by The go-to Tester (181 points)
Creating get and set methods is not the solution. You need to create debug point at very beginning and understand where your code is flowing.


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...