Commit 931c6f91 authored by 马博's avatar 马博

update

parent b4ddcda5
package base;
import com.codeborne.selenide.WebDriverRunner;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.*;
import ui.common.ProxyFactory;
import ui.common.WebDriverFactory;
import ui.common.WebDriverType;
import java.io.File;
import java.io.IOException;
import java.util.Set;
import static com.codeborne.selenide.Selenide.$;
/**
* Created by mabo on 2018/11/20
*/
public class DuibaBaseUi extends DuibaBase{
RemoteWebDriver driver= WebDriverFactory.createWebDriver(WebDriverType.MOBLIE, ProxyFactory.createProxy());
public ITestResult result;
public void switchChrome(){
WebDriverRunner.closeWebDriver();
driver= WebDriverFactory.createWebDriver(WebDriverType.CHRMOE, ProxyFactory.createProxy());
WebDriverRunner.setWebDriver(driver);
}
public void switchMobile(){
WebDriverRunner.closeWebDriver();
driver= WebDriverFactory.createWebDriver(WebDriverType.MOBLIE, ProxyFactory.createProxy());
WebDriverRunner.setWebDriver(driver);
}
@BeforeClass
public final void init(){
WebDriverRunner.setWebDriver(driver);
}
@BeforeMethod(alwaysRun = true)
public final void beforeMethodInit(ITestResult result) throws Exception {
Reporter.setCurrentTestResult(result);
this.result=result;
}
@AfterMethod(alwaysRun = true)
public final void afterMethodScreenshot() throws Exception {
if (!result.isSuccess())
screenshot();
}
public final void screenshot() throws IOException {
boolean ev=Boolean.parseBoolean(System.getProperty("ServiceDoctor"));
String path=null;
String url=null;
if(ev){
File file = new File("C:/image");
path = file.getAbsolutePath();
url = "http://172.16.80.106/";
}else{
File file = new File(" ");
path = file.getAbsolutePath();
url = file.getAbsolutePath();
}
Reporter.setCurrentTestResult(result);
// Reporter.log(file.getAbsolutePath());
String time = String.valueOf(System.currentTimeMillis());
Reporter.log("<img src='" + url + result.getName() +"_"+time+ ".jpg' hight='800' width='600' onclick=\"window.open('"+url + result.getName() +"_"+time+ ".jpg"+"')\" />");//"http://172.16.80.106"
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //执行屏幕截取
FileUtils.copyFile(srcFile, new File(path + result.getName() +"_"+time+ ".jpg"));
}
@AfterClass
public final void close(){
WebDriverRunner.closeWebDriver();
}
/**
* 等待
* @param time 秒
*/
public final void sleep(int time){
try {
Thread.sleep(time*1000);
logger.info("sleep:"+time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 点击通过text(),等于
* @param name
* @throws Exception
*/
public final void clickByTextEqual(String name) throws Exception{
String xpath = "//*[text()='"+name+"']";
int i=5;
while(i>0){
try {
$(By.xpath(xpath)).click();
logger.info("ByXpath:"+xpath+" click");
return;
} catch (Exception e) {
// TODO: handle exception
sleep(1);
i--;
if(i==0)throw e;
}
}
}
/**
* 点击通过text(),等于
* @param name
* @throws Exception
*/
public final void clickByClass(String name) throws Exception{
String xpath = "//*[@class='"+name+"']";
int i=5;
while(i>0){
try {
driver.findElementByXPath(xpath).click();
logger.info("xpath:"+xpath+" click");
return;
} catch (Exception e) {
// TODO: handle exception
sleep(1);
i--;
if(i==0)throw e;
}
}
}
/**
* 点击,通过属性,包含
* @param atttibut
* @param name
* @param tag
* @throws Exception
*/
public void clickByContainsAttribut(String atttibut,String name,String tag) throws Exception{
String xpath = "//"+tag+"[contains(@"+atttibut+",'"+name+"')]";
int i=3;
while(i>0){
try {
System.out.println(i);
driver.findElementByXPath(xpath).click();
logger.info("ByXpath:"+xpath+" click");
return;
} catch (Exception e) {
// TODO: handle exception
sleep(1);
i--;
if(i==0)throw e;
}
}
}
/**
* 切换窗口
*/
public void switchWindow(){
String handle = null;
Set<String> handles=driver.getWindowHandles();
for(String hand:handles) handle=hand;
driver.switchTo().window(handle);
}
}
package ui.cases.Exchange;
import base.DuibaBaseUi;
import org.openqa.selenium.By;
import org.testng.annotations.Test;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.open;
/**
* Created by mabo on 2018/11/21
*/
public class 优惠券_ExchangeTest_ui extends DuibaBaseUi{
@Test
public void 优惠券兑换_正常流程() throws Exception {
open("http://47.97.214.108/user/index");
clickByTextEqual("4762");
switchWindow();
sleep(2);
clickByClass("btn-close");
screenshot();
clickByTextEqual("ui自动化-优惠券");
screenshot();
clickByTextEqual("马上兑换");
screenshot();
clickByTextEqual("确定");
screenshot();
sleep(5);
screenshot();
}
}
package ui.cases.Exchange;
import base.DuibaBaseUi;
import org.testng.annotations.Test;
import static com.codeborne.selenide.Selenide.open;
/**
* Created by mabo on 2018/11/22
*/
public class 实物_ExchangeTest_ui extends DuibaBaseUi{
@Test
public void 实物兑换_正常流程() throws Exception {
open("http://47.97.214.108/user/index");
clickByTextEqual("4762");
switchWindow();
sleep(2);
clickByClass("btn-close");
screenshot();
clickByTextEqual("ui自动化-实物");
screenshot();
clickByTextEqual("马上兑换");
screenshot();
clickByTextEqual("确定");
screenshot();
sleep(5);
screenshot();
}
}
package ui.cases.Exchange;
import base.DuibaBaseUi;
import org.testng.annotations.Test;
import static com.codeborne.selenide.Selenide.open;
/**
* Created by mabo on 2018/11/22
*/
public class 支付宝_ExchangeTest_ui extends DuibaBaseUi{
@Test
public void 支付宝_正常流程() throws Exception {
open("http://47.97.214.108/user/index");
clickByTextEqual("4762");
switchWindow();
sleep(2);
clickByClass("btn-close");
screenshot();
clickByTextEqual("支付宝充值");
screenshot();
clickByTextEqual("马上兑换");
screenshot();
clickByTextEqual("确定");
screenshot();
sleep(5);
screenshot();
}
}
package ui.cases;
import base.DuibaBaseUi;
import com.codeborne.selenide.testng.ScreenShooter;
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.io.IOException;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.open;
/**
* Created by mabo on 2018/11/21
*/
public class uitest extends DuibaBaseUi{
@Test
public void 中文1() throws IOException {
ScreenShooter.captureSuccessfulTests = true;
//StatusCheckor checkor=new StatusCheckor();
// ProxyFactory.addCheckor(checkor);
// WebDriverRunner.setWebDriver(driver);
open("https://www.biying.com");
$(By.id("kw")).setValue("selenium");
Assert.assertEquals(1,2);
// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); //转换时间格式
// String time = dateFormat.format(Calendar.getInstance().getTime()); //获取当前时间
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment