import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class BrowserEngine {
public String browserName;
public String serverURL;
public WebDriver driver;
public String openstackLoginURL;
static public String openstackLoginUser;
static public String openstackLoginPwd;
public void initConfigData() throws IOException {
System.out.println("Initial Information:...");
Properties p = new Properties();
InputStream ips = new FileInputStream(".\\config\\config.properties");
p.load(ips);
browserName = p.getProperty("browserName");
System.out.println("Browser Name:" + browserName);
serverURL = p.getProperty("URL");
System.out.println("Server URL:" + serverURL);
openstackLoginURL = p.getProperty("openstackLoginURL");
System.out.println("openstackLoginURL:" + openstackLoginURL);
openstackLoginUser = p.getProperty("openstackLoginUser");
System.out.println("openstackLoginUser:" + openstackLoginUser);
openstackLoginPwd = p.getProperty("openstackLoginPwd");
System.out.println("openstackLoginPwd:" + openstackLoginPwd);
System.out.println("Initial Information Done...");
ips.close();
}
public WebDriver selectBrowser() {
System.out.println("Select Browser Start...");
if (browserName.equalsIgnoreCase("Firefox")) {
System.setProperty("webdriver.gecko.driver", ".\\Tools\\geckodriver.exe");
driver = createFireFoxDriver();
System.out.println("Selected Firefox...");
} else if (browserName.equalsIgnoreCase("Chrome")) {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\yanggaolei\\workspace\\DTTest\\Tools\\chromedriver.exe");
driver = new ChromeDriver();
System.out.println("Selected Chrome...");
} else if (browserName.equalsIgnoreCase("IE")) {
System.setProperty("webdriver.ie.driver", ".\\Tools\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
System.out.println("Selected IE...");
} else {
System.out.println("Wrong Input...Will Exit");
//
}
System.out.println("Get URL...");
driver.get(openstackLoginURL);
System.out.println("Maximize Windows...");
driver.manage().window().maximize();
System.out.println("Select Browser Done...");
return driver;
}
public void tearDown() throws InterruptedException {
driver.quit();
Thread.sleep(3000);
}
public void callWait(int time) {
driver.manage().timeouts().implicitlyWait(time, TimeUnit.SECONDS);
}
private WebDriver createFireFoxDriver() {
WebDriver driver = null;
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("prefs.converted-to-utf8", true);
firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference("browser.download.dir", ".\\TestDownload");
try {
driver = new FirefoxDriver(firefoxProfile);
} catch (Exception e) {
}
return driver;
}
public static void testWaitElementId(WebDriver driver, String id) {
// driver.get(url);
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id(id)));
WebElement element = driver.findElement(By.id(id));
((JavascriptExecutor) driver).executeScript("arguments[0].style.border = \"5px solid yellow\"", element);
}
public static void testWaitElementXpath(WebDriver driver, String xpath) {
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(xpath))); // wait
WebElement element = driver.findElement(By.xpath(xpath));
((JavascriptExecutor) driver).executeScript("arguments[0].style.border = \"5px solid yellow\"", element);
}
public static void testWaitElementCss(WebDriver driver, String css) {
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(css))); // wait
WebElement element = driver.findElement(By.cssSelector(css));
((JavascriptExecutor) driver).executeScript("arguments[0].style.border = \"5px solid yellow\"", element);
}
public static void scrollToBottom(WebDriver driver) throws InterruptedException {
((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
Thread.sleep(1000);
}
// JavascriptExecutor js = (JavascriptExecutor) driver;
// js.executeScript("window.scrollTo(100,450);");
// String keyword = (String) ((JavascriptExecutor)driver).executeScript("var
// input = document.getElementById(\"kw\").value;return input");
// System.out.println(keyword);
public void waitForPageLoad() throws InterruptedException {
int count=0;
while(!((JavascriptExecutor) driver).executeScript("return document.readyState" ).equals ("complete")){
Thread.sleep(1000);
count++;
if(count==10) break;
}
}
}
testng.xml :
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
<test name="DT_Openstack_test1" verbose="2">
<classes>
<class name="testP1.OpenstackLoginTest" />
</classes>
</test>
</suite>
build.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project name="DTTest" default="testoutput" basedir=".">
<property name="srcdir" location="${basedir}/src" />
<property name="libdir" location="${basedir}/lib" />
<property name="testng.output.dir" location="${basedir}/test-output" />
<property name="testng.file" value="testng.xml" />
<path id="classpath">
<fileset dir="${libdir}" includes="*jar" />
<fileset dir="${libdir}" includes="*zip" />
<pathelement location="${basedir}/bin" />
</path>
<!-- Define <testng> task -->
<taskdef name="testng" classname="org.testng.TestNGAntTask">
<classpath>
<pathelement location="./lib/org.testng_6.12.0.r201709030044.jar"/>
</classpath>
</taskdef>
<!-- Before compile,delete the previous folder -->
<target name="clean">
<delete dir="${basedir}/bin" />
</target>
<!-- Compile file in 'srcdir' to 'destdir' -->
<target name="compile" depends="clean">
<mkdir dir="${basedir}/bin" />
<javac srcdir="${srcdir}" encoding="UTF-8" destdir="${basedir}/bin" classpathref="classpath" includeantruntime="off" debug="on" debuglevel="lines,vars,source" />
</target>
<!-- use testng.xml to run the test -->
<target name="runtest" depends="compile">
<testng outputdir="${testng.output.dir}" classpathref="classpath">
<xmlfileset dir="${basedir}" includes="${testng.file}"/>
</testng>
</target>
<!-- Use xslt to modify the report -->
<target name= "testoutput" depends="runtest" >
<xslt in= "${testng.output.dir}/testng-results.xml"
style= "${testng.output.dir}/testng-results.xsl"
out= "${testng.output.dir}/index1.html " >
<param name= "testNgXslt.outputDir" expression= "${basedir}/test-output/" />
<param name="testNgXslt.showRuntimeTotals" expression="true" />
<classpath refid= "classpath" />
</xslt>
</target>
</project>
package pageObjects;
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class BasePage {
public static WebDriver driver;
public static String pageTitle;
public static String pageUrl;
public BasePage(WebDriver driver) {
BasePage.driver = driver;
}
protected void type(WebElement element, String text) {
try {
if (element.isEnabled()) {
element.clear();
element.sendKeys(text);
}
} catch (Exception e) {
}
}
protected void click(WebElement element) {
try {
if (element.isEnabled()) {
element.click();
}
} catch (Exception e) {
}
}
protected void clean(WebElement element) {
try {
if (element.isEnabled()) {
element.clear();
}
} catch (Exception e) {
}
}
protected void verifyElementIsPresent(WebElement element) {
try {
if (element.isDisplayed()) {
}
} catch (Exception e) {
}
}
protected String getCurrentPageTitle() {
pageTitle = driver.getTitle();
return pageTitle;
}
protected String getCurrentPageUrl() {
pageUrl = driver.getCurrentUrl();
return pageUrl;
}
public void switchWindow() {
String currentWindow = driver.getWindowHandle();
Set<String> handles = driver.getWindowHandles();
Iterator<String> it = handles.iterator();
while (it.hasNext()) {
if (currentWindow == it.next()) {
continue;
}
try {
driver.close();
WebDriver window = driver.switchTo().window(it.next()); // switch to new window
} catch (Exception e) {
}
}
}
}
config.properties :
browserName=Chrome
URL=https://www.baidu.com/
openstackLoginURL=http://192.168.140.10/dashboard
openstackLoginUser=OMC
openstackLoginPwd=Changeme_123
package pageObjects;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class ManoHomePage extends BasePage{
public ManoHomePage(WebDriver driver) {
super(driver);
}
@FindBy(xpath=TemplateXpath)
public WebElement Template;
public static final String TemplateXpath="/html/body/header/div/div/nav[1]/ul/li[1]/a";
@FindBy(xpath=SliceMangementXpath)
public WebElement SliceManagement;
public static final String SliceMangementXpath="/html/body/header/div/div/nav[1]/ul/li[2]";
@FindBy(xpath=SliceManagementEntityXpath)
public WebElement SliceManagementEntity;
public static final String SliceManagementEntityXpath="//aside/div[2]/dl/dt"; //*[@id="menu-aaaaa"]/dt
@FindBy(xpath=SliceManagementEntitySliceXpath)
public WebElement SliceManagementEntitySlice;
public static final String SliceManagementEntitySliceXpath="//aside/div[2]/dl/dd";
//the new button is in this iframe, check it in DOM
@FindBy(xpath=SliceManagementEntitySliceTargetIframeXpath)
public WebElement SliceManagementEntitySliceTargetIframe;
public static final String SliceManagementEntitySliceTargetIframeXpath="//*[@id=\"iframe_box\"]/div/iframe[@src=\"html/lifecycle/lifecycle-Slice.html\"]";
@FindBy(xpath=ReourceManagementPhysicalDevIframeXpath)
public WebElement ReourceManagementPhysicalDevIframe;
public static final String ReourceManagementPhysicalDevIframeXpath="//*[@id=\"iframe_box\"]/div/iframe[@src=\"html/resource/resource-physical.html\"]";
@FindBy(xpath=ReourceManagementPhysicalDevVCPUXpath)
public WebElement ReourceManagementPhysicalDevVCPU;
public static final String ReourceManagementPhysicalDevVCPUXpath="//*[@id=\"Vcpu\"]/div[1]/canvas";
@FindBy(xpath=SliceManagementEntitySliceSliceTabXpath)
public WebElement SliceManagementEntitySliceSliceTab;
public static final String SliceManagementEntitySliceSliceTabXpath="//*[@id=\"min_title_list\"]/li[2]";
@FindBy(xpath=SliceManagementEntitySliceFirstTabXpath)
public WebElement SliceManagementEntitySliceFirstTab;
public static final String SliceManagementEntitySliceFirstTabXpath="//section/div/div/ul/li[1]";
@FindBy(xpath=SliceManagementEntitySliceSliceTabNewXpath)
public WebElement SliceManagementEntitySliceSliceTabNew;
public static final String SliceManagementEntitySliceSliceTabNewXpath="/html/body/div[1]/div/span";
@FindBy(xpath=ResouceManagementXpath)
public WebElement ResouceManagement;
public static final String ResouceManagementXpath="/html/body/header/div/div/nav[1]/ul/li[3]/a";
@FindBy(xpath=ResouceManagementLogicalDevXpath)
public WebElement ResouceManagementLogicalDev;
public static final String ResouceManagementLogicalDevXpath="/html/body/aside/div[3]/ul/li[2]/a";
@FindBy(xpath=ResouceManagementPhysicalDevXpath)
public WebElement ResouceManagementPhysicalDev;
public static final String ResouceManagementPhysicalDevXpath="/html/body/aside/div[3]/ul/li[1]/a";
@FindBy(xpath=ResouceManagementPhysicalDevDC3Xpath)
public WebElement ResouceManagementPhysicalDevDC3;
public static final String ResouceManagementPhysicalDevDC3Xpath="//*[@id=\"Huifold1\"]/li[3]/h4";
@FindBy(xpath=ResouceManagementPhysicalDevDC3_2Xpath)
public WebElement ResouceManagementPhysicalDevDC3_2;
public static final String ResouceManagementPhysicalDevDC3_2Xpath="//*[@id=\"computer37\"]";
@FindBy(xpath=AccountXpath)
public WebElement Account;
public static final String AccountXpath="//*[@id=\"Hui-userbar\"]/ul/li[2]/a";
@FindBy(xpath=AccountPersonalInfoXpath)
public WebElement AccountPersonalInfo;
public static final String AccountPersonalInfoXpath="//*[@id=\"Hui-userbar\"]/ul/li[2]/ul/li[1]";
//this id changes every time!
@FindBy(xpath=AccountPersonalInfoCloseXpath)
public WebElement AccountPersonalInfoClose;
public static final String AccountPersonalInfoCloseXpath="//div[starts-with(@id,'layui-layer')][contains(@class, 'layer-anim')]/span/a[3]";
@FindBy(xpath=ManoPageFootXpath)
public WebElement ManoPageFoot;
public static final String ManoPageFootXpath="/html/body/footer/div/p";
@FindBy(xpath=DTMobileXpath)
public WebElement DTMobile;
public static final String DTMobileXpath="/html/body/header/div/div/a[1]";
}
package testP1;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import framework.BrowserEngine;
import pageObjects.OpenstachMainPage;
import pageObjects.OpenstackLoginPage;
public class OpenstackLoginTest {
public WebDriver driver;
BrowserEngine browserEngine = new BrowserEngine();
//these test cases are just for demo! not real test cases!
@Test(enabled=true)
public void testPopUp() throws InterruptedException {
OpenstackLoginPage openstackLoginPage = PageFactory.initElements(driver, OpenstackLoginPage.class);
OpenstachMainPage openstachMainPage = openstackLoginPage.OpenstackLoginPageLogin();
//wait for jump
BrowserEngine.testWaitElementXpath(driver, openstachMainPage.projectXpath);
openstachMainPage = PageFactory.initElements(driver, OpenstachMainPage.class);
BrowserEngine.testWaitElementXpath(driver, openstachMainPage.instanceInfoXpath);
openstachMainPage.instanceInfo.click();
BrowserEngine.testWaitElementXpath(driver, OpenstachMainPage.instanceInfoCreateInstXpath);
openstachMainPage.instanceInfoCreateInst.click();
BrowserEngine.testWaitElementXpath(driver, OpenstachMainPage.instanceInfoCreateInstInstanceTypeXpath);
openstachMainPage.instanceInfoCreateInstInstanceType.click();
BrowserEngine.testWaitElementXpath(driver, OpenstachMainPage.instanceInfoCreateInstNextXpath);
openstachMainPage.instanceInfoCreateInstNext.click();
BrowserEngine.testWaitElementXpath(driver, OpenstachMainPage.instanceInfoCreateInstCloseXpath);
openstachMainPage.instanceInfoCreateInstClose.click();
Thread.sleep(5000);
}
@Test(enabled=false)
public void testScrollDown() throws InterruptedException {
OpenstackLoginPage openstackLoginPage = PageFactory.initElements(driver, OpenstackLoginPage.class);
OpenstachMainPage openstachMainPage = openstackLoginPage.OpenstackLoginPageLogin();
//wait for jump
BrowserEngine.testWaitElementXpath(driver, openstachMainPage.projectXpath);
openstachMainPage = PageFactory.initElements(driver, OpenstachMainPage.class);
BrowserEngine.testWaitElementXpath(driver, openstachMainPage.generalInfoXpath);
openstachMainPage.generalInfo.click();
BrowserEngine.scrollToBottom(driver);
BrowserEngine.testWaitElementXpath(driver, openstachMainPage.generalInfoSumXpath);
System.out.println("get sum num:" + openstachMainPage.getSumNum(openstachMainPage.getSumText()));
String sumNum=openstachMainPage.getSumNum(openstachMainPage.getSumText());
//assert result
Assert.assertEquals(sumNum, "26");
// Thread.sleep(5000);
}
@Test(enabled=false)
public void testAPISum() throws InterruptedException {
OpenstackLoginPage openstackLoginPage = PageFactory.initElements(driver, OpenstackLoginPage.class);
OpenstachMainPage openstachMainPage = openstackLoginPage.OpenstackLoginPageLogin();
//wait for jump
BrowserEngine.testWaitElementXpath(driver, openstachMainPage.projectXpath);
openstachMainPage = PageFactory.initElements(driver, OpenstachMainPage.class);
BrowserEngine.testWaitElementXpath(driver, openstachMainPage.instanceInfoXpath);
openstachMainPage.instanceInfo.click();
BrowserEngine.testWaitElementXpath(driver, openstachMainPage.instanceInfoNameXpath);
openstachMainPage.accessAPI.click();
BrowserEngine.testWaitElementXpath(driver, openstachMainPage.accessAPISumNumXpath);
System.out.println("get sum num:" + openstachMainPage.getSumNum(openstachMainPage.getSumText()));
String sumNum=openstachMainPage.getSumNum(openstachMainPage.getSumText());
//assert result
Assert.assertEquals(sumNum, "9");
Thread.sleep(5000);
}
@Test(enabled=false)
public void testSecurityGroup() throws InterruptedException {
OpenstackLoginPage openstackLoginPage = PageFactory.initElements(driver, OpenstackLoginPage.class);
OpenstachMainPage openstachMainPage = openstackLoginPage.OpenstackLoginPageLogin();
//wait for jump
BrowserEngine.testWaitElementXpath(driver, openstachMainPage.projectXpath);
//initialize main page
openstachMainPage = PageFactory.initElements(driver, OpenstachMainPage.class);
BrowserEngine.testWaitElementXpath(driver, openstachMainPage.networkPartXpath);
openstachMainPage.networkPart.click();
Thread.sleep(1500);
BrowserEngine.testWaitElementXpath(driver, openstachMainPage.networkPartSecurityGroupXpath);
openstachMainPage = PageFactory.initElements(driver, OpenstachMainPage.class);
openstachMainPage.networkPartSecurityGroup.click();
Thread.sleep(1500);
BrowserEngine.testWaitElementXpath(driver, openstachMainPage.networkPartSecurityGroupSumTextXpath);
openstachMainPage = PageFactory.initElements(driver, OpenstachMainPage.class);
System.out.println("get sum num:" + openstachMainPage.getSumNum(openstachMainPage.getNetworkPartSecurityGroupSumText()));
String sumNum=openstachMainPage.getSumNum(openstachMainPage.getNetworkPartSecurityGroupSumText());
//assert the result
Assert.assertEquals(sumNum, "1");
//role part
openstachMainPage.rolePart.click();
Thread.sleep(2000);
BrowserEngine.testWaitElementXpath(driver, openstachMainPage.rolePartProjectXpath);
openstachMainPage = PageFactory.initElements(driver, OpenstachMainPage.class);
openstachMainPage.rolePartProject.click();
BrowserEngine.testWaitElementXpath(driver,openstachMainPage.rolePartProjectlabelXpath);
openstachMainPage.rolePartProjectlabel.click();
Thread.sleep(5000);
}
@BeforeClass
public void setUp() throws IOException {
System.out.println("Before Class Start...");
browserEngine.initConfigData();
driver = browserEngine.selectBrowser();
}
@AfterClass
public void tearDown() {
driver.quit();
System.out.println("Driver Quite...");
}
}
以下链接涵盖了目前所有疑问的答案:
http://www.51testing.com/html/87/300987-831171.html
https://pan.baidu.com/s/1c0uFX8O?errno=0&errmsg=Auth%20Login%20Sucess&&bduss=&ssnerror=0
http://blog.csdn.net/sxl0727tu/article/details/51893032
http://blog.csdn.net/sxl0727tu/article/details/51896208
http://blog.csdn.net/jlminghui/article/details/50477283
http://blog.csdn.net/kittyboy0001/article/details/25219053
http://blog.csdn.net/erix1991/article/details/42423387
http://www.cnblogs.com/qingchunjun/p/4208159.html
http://chromedriver.storage.googleapis.com/index.html
http://blog.csdn.net/huilan_same/article/details/51896672
http://blog.csdn.net/u011541946/article/details/75393386
http://blog.csdn.net/guoguo527/article/details/52414100
http://www.cnblogs.com/zmhsoup/p/5249663.html
http://www.tutorialspoint.com/selenium/selenium_test_ng.htm