import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Calendar;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;
public class HelperClass
{
private String fileName;
private String browser;
private String chromeDriverPath;
private String ieDriverPath;
private WebDriver driver;
private Properties prop;
private String serverHost;
private String slowdownSeconds;
private String implicitlyWaitTimeOut;
private String implicitlyWaitForSpecialTimeOut;
private String testingSiteURL;
private String loginUser;
private String loginPwd;
private CVData cvData;
private CVGUI cvGui;
private String myTestStr;
private String randomNum;
/**
*
*/
public HelperClass() {
// TODO Auto-generated constructor stub
super();
}
public void setup()
{
//super.setup();
InputStream propFile;
fileName = new String("test.properties");
prop = new Properties();
try
{
propFile = new FileInputStream("C:/automation/tests/auto_proj/" + fileName);
prop.load(propFile);
this.serverHost = prop.getProperty("serverHost");
this.browser = prop.getProperty("browser");
System.out.println("The browser is: " + browser + "\n");
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()) + "\n");
this.chromeDriverPath = prop.getProperty("chromeDriverPath");
this.ieDriverPath = prop.getProperty("ieDriverPath");
this.implicitlyWaitTimeOut = prop.getProperty("implicitlyWaitTimeOut");
this.implicitlyWaitForSpecialTimeOut = prop.getProperty("implicitlyWaitForSpecialTimeOut");
System.out.println("The implicitlyWaitTimeOut is: " + this.implicitlyWaitTimeOut + " seconds\n");
System.out.println("The implicitlyWaitForSpecialTimeOut is: " + this.implicitlyWaitForSpecialTimeOut + " mili-seconds\n");
propFile.close();
}
catch (IOException e)
{
System.out.println("[ERROR]: Failed loading property file.");
System.out.println(e.toString());
System.exit(1);
}
try
{
this.testDataAndGuiInit();
}
catch (Exception e)
{
System.out.println("\n[ERROR]: Failed initializing test data and gui.\n");
System.exit(1);
}
System.out.println("CVData.myStr is: " + CVData.myStr + "\n");
System.out.println("CVGUI.myGuiStr is: " + CVGUI.myGuiStr + "\n");
try
{
if (!this.serverHost.equalsIgnoreCase("none"))
{
if (this.browser.equalsIgnoreCase("chrome"))
{
driver = new BaseRemoteWebDriver(new URL("http://" + this.serverHost + ":4444/wd/hub/"), DesiredCapabilities.chrome());
}
else if (this.browser.equalsIgnoreCase("ie"))
{
driver = new BaseRemoteWebDriver(new URL("http://" + this.serverHost + ":4444/wd/hub/"), DesiredCapabilities.internetExplorer());
}
else if (this.browser.equalsIgnoreCase("ff"))
{
driver = new BaseRemoteWebDriver(new URL("http://" + this.serverHost + ":4444/wd/hub/"), DesiredCapabilities.firefox());
}
System.out.println("The serverHost running remote is: " + prop.getProperty("serverHost") + "\n");
}
else
{
if (this.browser.equalsIgnoreCase("chrome"))
{ //我们需要对 chrome 做一些特殊的设置,以完成我们期望的浏览器行为.
ChromeOptions options=new ChromeOptions();
Map<String, Object> prefs=new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0); //禁止弹出窗口
prefs.put("download.default_directory", "C:/automation"); //设置下载路径
System.setProperty("webdriver.chrome.driver", this.chromeDriverPath);
DesiredCapabilities cap=DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
System.out.println("The System Propery is: " + System.getProperty("webdriver.chrome.driver").toString() + "\n");
driver=new BaseChromeDriver(cap);
}
else if (this.browser.equalsIgnoreCase("ie"))
{
System.setProperty("webdriver.ie.driver", this.ieDriverPath);
System.out.println("The System Propery is: " + System.getProperty("webdriver.ie.driver").toString() + "\n");
driver = new BaseInternetExplorerDriver();
}
else if (this.browser.equalsIgnoreCase("ff"))
{
driver = new BaseFirefoxDriver();
}
System.out.println("The server is running local\n");
}
driver.manage().timeouts().implicitlyWait(this.getImplicitlyWaitTimeOut(), TimeUnit.SECONDS);
}
catch (Exception e)
{
System.out.println("[ERROR]: Faile creating driver.");
System.out.println(e.toString());
System.exit(1);
}
}
public void testDataAndGuiInit()
{
String lang = prop.getProperty("language");
if (lang == null)
{
System.out.println("\n\n\n\n\n[ERROR]: Please set language in test.properties first, thanks!\n\n\n\n\n");
System.exit(1);
}
System.out.println("The current language setting is: " + lang + "\n");
if (lang.equalsIgnoreCase("Eng"))
{
cvData = new CVData(); //default English
cvGui = new CVGUI(); //default English
}
else if (lang.equalsIgnoreCase("Sch"))
{
cvData = new CVData("Sch"); //Simplified Chinese
cvGui = new CVGUI("Sch"); //Simplified Chinese
}
else
{
//Leave the option for the future
}
}
public WebDriver getWebDriver()
{
return driver;
}
}
========================================================
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestV{
@BeforeClass
public static void setUpBeforeClass() throws Exception {
helper = new HelperClass();
helper.setup();
driver = helper.getWebDriver();
driver.manage().window().maximize();
driver.get(“https://www.bvtest.com”);
....
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
driver.quit();
}
}