如何在基类中的每个测试中调用一个方法,并在selenium中有一个数据提供者

时间:2018-02-15 17:37:48

标签: java selenium selenium-webdriver selenium-chromedriver browserstack

我一直在尝试将浏览器堆栈与我的selenium脚本集成。作为其中的一部分,我已经在我的“getBrowser”中添加了所需的功能。 Base类中使用数据提供程序的方法。因为我想在多个浏览器中运行我的脚本。浏览器列表位于" getData"基类中的方法。

如何在我的测试用例中调用getBrowser方法,并且只在基类中定义getData(浏览器列表)。这就是我所做的,也是不对的。我添加了我的基类和测试脚本。

Base.getBrowser(平台平台,String browserName,String browserVersion); 这条线是我卡住了。

任何帮助将不胜感激。谢谢



package com.gale.precision.FundVisualizer.core;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.DataProvider;
import org.openqa.selenium.Platform;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Base {

  //public static WebDriver driver = null;
  public static WebDriver driver;
  public static String DriverPath = System.getProperty("user.dir") + "//" + "Drivers";
  public static String DirectoryPath = System.getProperty("user.dir");
  public static Properties prop = new Properties();
  public static InputStream input = null;

  public static final String USERNAME = "antonyprabhu1";
  public static final String AUTOMATE_KEY = "xHRMpqxgD8sn3e3sr75s";

  public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";

  @DataProvider(name = "EnvironmentDetails")
  public static void getBrowser(Platform platform, String browserName, String browserVersion) throws MalformedURLException

  {

    DesiredCapabilities capability = new DesiredCapabilities();
    capability.setPlatform(platform);
    capability.setBrowserName(browserName);
    capability.setVersion(browserVersion);
    capability.setCapability("browserstack.debug", "true");

    driver = new RemoteWebDriver(new URL(URL), capability);
    try {
      input = new FileInputStream(DirectoryPath + "//" + "config" + "//" + "app.properties");
      prop.load(input);

    } catch (IOException e) {
      e.printStackTrace();
    }

  }



  @DataProvider(name = "EnvironmentDetails", parallel = true)
  public Object[][] getData() {

    Object[][] testData = new Object[][] {
      {
        Platform.MAC, "chrome", "62.0"
      }, {
        Platform.WIN8,
        "chrome",
        "62.0"
      }, {
        Platform.WINDOWS,
        "firefox",
        "57"
      }
    };

    return testData;
  }

  public static void closeBrowser() {
    driver.quit();
  }

}






package comparison;




import java.net.MalformedURLException;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Platform;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.gale.precision.FundVisualizer.core.Base;
import com.gale.precision.FundVisualizer.core.ExtentReport;
import com.gale.precision.FundVisualizer.pageObject.Comparison;
import com.gale.precision.FundVisualizer.pageObject.InvestmentsSearch;

@SuppressWarnings("unused")
public class AddedInvestmentDisplay extends ExtentReport {

  /*============================================================================================================================

   Test case : Verify that already selected Investments for comparison does not show up  
   ======================================================================================*/





  @Test(testName = "Comparison: Verify an already selected Investment for comparison does not show up in search")
  public void verifyAddedInvestmentDisplay() throws InterruptedException, MalformedURLException {

    //test = extent.createTest(Thread.currentThread().getStackTrace()[1].getMethodName());

    Base.getBrowser(Platform platform, String browserName, String browserVersion);
    InvestmentsSearch.login(Base.driver);
    InvestmentsSearch.InvestmentsLink(Base.driver).click();
    JavascriptExecutor jse = (JavascriptExecutor) Base.driver;
    jse.executeScript("window.scrollBy(0,750)", "");
    InvestmentsSearch.ViewResults(Base.driver).click();



    for (int i = 0; i <= 2; i++)

    {

      try {
        Comparison.firstCheckBox(Base.driver).click();
        break;

      } catch (Exception e) {

        System.out.println(e.getMessage());

      }
    }

    //Base.clickingStaleElements(Comparison.firstCheckBox(Base.driver));

    //Comparison.firstCheckBox(Base.driver).click();



    for (int i = 0; i <= 2; i++)

    {

      try {
        Comparison.analyzeOrCompare(Base.driver).click();
        break;

      } catch (Exception e) {

        System.out.println(e.getMessage());

      }
    }

    Comparison.addInvestmentField(Base.driver).sendKeys("MFC027");

    Comparison.firstSearchResult(Base.driver).click();

    Comparison.addInvestmentField(Base.driver).sendKeys("MFC027");

    Assert.assertEquals(Comparison.emptySearchResult(Base.driver).isDisplayed(), true);



  }

  @DataProvider(name = "EnvironmentDetails", parallel = true)
  public Object[][] getData() {

    Object[][] testData = new Object[][] {
      {
        Platform.MAC, "chrome", "62.0"
      }, {
        Platform.WIN8,
        "chrome",
        "62.0"
      }, {
        Platform.WINDOWS,
        "firefox",
        "57"
      }
    };

    return testData;
  }




  @AfterClass
  public void tearDown()

  {
    Base.closeBrowser();

  }
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我可以通过传递@test方法

中的参数来解决这个问题

&#13;
&#13;
 @Test(testName="Comparison: Verify adding an index in the comparison", dataProvider = "EnvironmentDetails",dataProviderClass = BrowsersDataProvider.class)
  public void verifyAddingIndex(Platform platform, String browserName, String browserVersion) throws InterruptedException, MalformedURLException {
	  
	    //test = extent.createTest(Thread.currentThread().getStackTrace()[1].getMethodName());
	 
	    Base.getBrowser(platform,browserName,browserVersion);
		InvestmentsSearch.login(Base.driver);
		
		
&#13;
&#13;
&#13;