asd
asd

Reputation: 1

Parallel = "tests" not working in Grid

Following is my Methods code :

package testcases;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import pages.ResultsPage;
import utilities.ExcelReader;

public class methodDefinations extends base {

    public void tearDown(){
        driver.quit();
    }

    public void setUp(String browser) throws MalformedURLException {
        System.out.println(browser);
        DesiredCapabilities capabilities = null;
        System.out.println("before driver==null")
        if(browser.equals("firefox")){
            System.out.println("ff");
            capabilities = DesiredCapabilities.firefox();
            capabilities.setBrowserName("firefox");
            capabilities.setPlatform(Platform.ANY); 
        }
        else if(browser.equals("chrome")){
            System.out.println("ch");
            capabilities = DesiredCapabilities.chrome();
            capabilities.setBrowserName("chrome");
            capabilities.setPlatform(Platform.ANY);
        }
        System.out.println("inside driver==null");
        driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities); 
        System.out.println(capabilities);
        String browserName = capabilities.getBrowserName();
        String browserVersion = capabilities.getVersion();
        System.out.println("Browser = " + browserName + " "+browserVersion);
        System.out.println("before testsiteurl");
        driver.get("http://keep.google.com");
        System.out.println("after testsite url");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
    }

    public void doLogin()
    {
        System.out.println("after maximize");
        driver.findElement(By.cssSelector("#Email")).sendKeys("[email protected]");
        driver.findElement(By.cssSelector("#next")).click();
        driver.findElement(By.cssSelector("#Passwd")).sendKeys("q1w2e3r4t5y6u7i8o9p0");
        driver.findElement(By.cssSelector("#signIn")).click();

        driver.findElement(By.xpath("html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div[1]/div[7]")).sendKeys("example");
        System.out.println("example typed");
    }
}

Following is my Test Case : package testcases;

import java.net.MalformedURLException;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;    
import utilities.ExcelReader;

public class testCasess {
    @Parameters("browser")
    @Test
    public void setup(String browser) throws MalformedURLException, InterruptedException {
        methodDefinations md = new methodDefinations();
    md.setUp(browser);
}

@Test
public void login()
{
    methodDefinations md = new methodDefinations();
    md.doLogin();
    md.tearDown();
}

Following is my XML :

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    <suite name="Suite" parallel="tests" thread-count="2" >
    <test name="LoginTest">
    <parameter name = "browser" value= "firefox"></parameter>
        <classes>
            <class name="testcases.testCasess"/> 
        </classes>
    </test> <!-- Test -->
    <test name="LoginTest2">
    <parameter name = "browser" value= "chrome"></parameter>
    <classes>
       <class name="testcases.testCasess"/>
    </classes>
    </test> <!-- Test --> 
    </suite> <!-- Suite -->

Parallel = "tests" not working in Grid 2. It basically opens both the browsers. But when it comes to typing username, it types username in firefox twice. Nothing is typed in Chrome. I dont understand why this is happening.

Following is the Base class :

    package testcases;

import java.io.FileInputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;


public class base {

    public WebDriver driver=null;
    public static WebDriverWait wait;

    public static Properties OR = new Properties();
    public static Properties Config = new Properties();
    public static Logger log = Logger.getLogger("devpinoyLogger");
    public static FileInputStream fis;



    public void tearDown()
    {
        driver.quit();
    }



    }


}

Upvotes: 0

Views: 191

Answers (2)

asd
asd

Reputation: 1

My Base class is as follows :

package base;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Parameters;


public class TestBase {

    public static WebDriver driver;
    public static Properties OR = new Properties();
    public static Properties Config = new Properties();
    //public static Logger log = Logger.get
    public static FileInputStream fis;



    @BeforeSuite
    @Parameters({ "browser" })
    public void setUp(String browser) throws MalformedURLException{
        if(driver==null)
        {
            try {
                fis = new FileInputStream(System.getProperty("user.dir")+"/src/test/resources/properties/OR.properties");
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                OR.load(fis);
                //log.debug("OR file loaded");

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                fis = new FileInputStream(System.getProperty("user.dir")+"/src/test/resources/properties/Config.properties");
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                Config.load(fis);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            System.out.println("*******************");
            driver = Browser.getDriver(browser);

            driver.get(Config.getProperty("testsiteurl"));

            driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);
            driver.manage().window().maximize();

            //WebDriverWait wait = new WebDriverWait(driver, 10L);




        }


    }

    @AfterSuite
    public void tearDown(){
        System.out.println("Closing browser");
        driver.quit();

    }

}

Upvotes: 0

niharika_neo
niharika_neo

Reputation: 8531

Based on your code, this can only happen if your driver is static. Basically, you should have two instances of driver to interact with them separately.

Upvotes: 1

Related Questions