并行="测试"不在Grid中工作

时间:2016-11-29 12:33:50

标签: selenium testng selenium-grid

以下是我的方法代码:

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("abcd@gmail.com");
        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");
    }
}

以下是我的测试用例:     包装测试箱;

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

以下是我的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 -->

平行=&#34;测试&#34;不在Grid 2中工作。 它基本上打开了两个浏览器。但是当输入用户名时,它会在firefox中输入两次用户名。 Chrome中没有输入任何内容。 我不明白为什么会这样。

以下是基类:

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



    }


}

2 个答案:

答案 0 :(得分:1)

根据您的代码,只有在您的驱动程序是静态的情况下才会发生这种情况。基本上,您应该有两个驱动程序实例分别与它们进行交互。

答案 1 :(得分:0)

我的基类如下:

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();

    }

}