5、编写代码示例
我这里就比较简单介绍一下我的操作步骤。
(1)新建一个maven项目(怎么新建请自行百度)
我的目录结构如图所示:
下面是我的用例代码的编写,写个三个Test,从注解就能看出来。当然中间牵扯到调用其他类、调用文件、继承其他类等问题,我就不全部粘贴出来了。
@Test
//打开首页
public void openPage(){
driver.get(“这里是打开的地址”);
waitTime(2000);
driver.manage().window().maximize();
waitTime(2000);
}
@Test(dependsOnMethods={"openPage"})
//账号登录
public void Login(){
this.propertiesfile();
WebElement name = driver.findElement(By.id(prop.getProperty("user")));
name.sendKeys(prop.getProperty("uservalue"));
waitTime(1000);
WebElement passwo = driver.findElement(By.id(prop.getProperty("passwo")));
passwo.sendKeys(prop.getProperty("passwovalue"));
waitTime(1000);
WebElement Loginbutton = driver.findElement(By.id(prop.getProperty("login")));
Loginbutton.click();
waitTime(3000);
String e1 = driver.findElement(By.id("login-name")).getText();
System.out.println("当前登录人是: "+e1);
}
@Test(dependsOnMethods={"Login"})
//新增供应商
public void Addgys(){
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();",driver.findElement(By.linkText("供应商管理")));
js.executeScript("arguments[0].click();",driver.findElement(By.linkText("供应商列表")));
WebElement iframe = driver.findElement(By.id("/cpkhe/supplier/list"));
driver.switchTo().frame(iframe);
WebDriverWait wait1 = new WebDriverWait(driver,30);
wait1.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(prop.getProperty("addgysbutton"))));
WebElement gys3 = driver.findElement(By.cssSelector(prop.getProperty("addgysbutton")));
gys3.click();
waitTime(2000);
driver.switchTo().defaultContent();
WebElement iframe1 = driver.findElement(By.id("/cpkhe/supplier/add"));
driver.switchTo().frame(iframe1);
driver.findElement(By.id("supName")).sendKeys("Auto供应商名称1");;
driver.findElement(By.id("firstContact")).sendKeys("第一联系人");
driver.findElement(By.id("firstContactPost")).sendKeys("第一联系人职务");
driver.findElement(By.id("firstContactMobile")).sendKeys("18892080505");
driver.findElement(By.id("contactEmail")).sendKeys("9072388@qq.com");
driver.findElement(By.id("sourceInfo")).sendKeys("渠道信息渠道信息渠道信息啦啦啦啦");
driver.findElement(By.id("brandName0")).sendKeys("品牌名称1");
driver.findElement(By.cssSelector(".ant-select-selection__placeholder")).click();
driver.findElement(By.xpath(prop.getProperty("branddowm"))).click();
driver.findElement(By.id("applyReason")).sendKeys("申请理由申请理由");
driver.findElement(By.cssSelector("span.ant-upload > input:nth-child(1)")).sendKeys("C:\\Users\\ye.menghua\\Desktop\\红星美凯龙\\图片\\timg.jpg");
waitTime(2000);
driver.findElement(By.cssSelector("button.ant-btn:nth-child(2)")).click();
WebDriverWait wait2 = new WebDriverWait(driver,30);
wait2.until(ExpectedConditions.presenceOfElementLocated(By.className("message-info-desc")));
String text1 = driver.findElement(By.className("message-info-desc")).getText();
System.out.println("新增供应商提交之后,提示信息:"+text1);
}
//时间等待
public static void waitTime(int time){
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
(2)项目需要的包都在pom文件中添加依赖,只需要在maven仓库(mvnrepository.com)中搜索需要的包,添加到pom文件中即可。
以下代码是我的pom.xml文件中添加的依赖的内容:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.1</version>
</dependency>
<dependency>
<groupId>velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.21</version>
<scope>test</scope>
</dependency>
<!-- 依赖reportNg 关联testNg-->
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.2</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 依赖Guice -->
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
<plugins>
<!-- 添加插件,添加ReportNg的监听器,修改最后的TestNg的报告 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<properties>
<property>
<name>usedefaultlisteners</name>
<value>false</value>
</property>
<property>
<name>listener</name>
<value>org.uncommons.reportng.HTMLReporter, org.uncommons.reportng.JUnitXMLReporter</value>
</property>
</properties>
<workingDirectory>target/</workingDirectory>
<forkMode>always</forkMode>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
6、生成testng.xml文件
(1)右键点击项目名称,TestNg→Convert to TestNg,接着点击Next→finish。就会生成testng.xml文件。
(2)可以右键点击testng.xml文件运行用例。
(3)也可以在pom文件中添加下图内容,关联testng.xml文件。
此时执行pom文件也可执行用例。
(4)生成的报告在target\surefire-reports\html目录下的index.html文件中,这里要说明一下,我的pom文件中添加可reportng的依赖,所有报告的样式是按照以下样式生成的。
7、jenkins环境配置
将jenkins搭建在本地的tomcat中,首先要搭建tomcat环境,在官网上下载tomcat压缩包,解压,配置tomcat环境变量完成之后,启动tomcat的bin目录下的startup.bat文件,出现如图所示的窗口。
如果没有更改端口号的话,默认打开地址为http://localhost:8080,如图所示:
下载jenkins.war包,下载成功之后,将改文件放在tomcat的webapp目录下,此时重启tomcat,在地址栏中输入http://localhost:8080/jenkins/可打开jenkins页面。
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理