51Testing软件测试论坛
标题:
完整的第一个后台selenium Demo
[打印本页]
作者:
阿蛮的开心姐
时间:
2018-3-26 14:39
标题:
完整的第一个后台selenium Demo
package chrome;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class back_end {
ChromeDriverService service;
WebDriver driver;
@Before
public void before(){
service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("D:\\Python\\WorkSpace\\chrome\\res\\chromedriver.exe"))
.usingAnyFreePort().build();
try {
service.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver = new RemoteWebDriver(service.getUrl(), DesiredCapabilities.chrome());
}
public void login() {
driver.get("http://192.168.2.252/loyaltea-cms/login.jsp");
WebElement username=driver.findElement(By.xpath("//*[@id='username']"));
username.sendKeys("admin");
WebElement pwd=driver.findElement(By.xpath("//*[@id='password']"));
pwd.sendKeys("qctadmin");
WebElement yanzhengma=driver.findElement(By.id("code"));
yanzhengma.sendKeys("8888");
WebElement login_btn=driver.findElement(By.xpath("//*[@id='login-form']/p[5]/button"));
login_btn.click();
}
//商城管理商城订单
@Test
public void mallorder() throws InterruptedException{
login();
WebElement shangchengguanli=driver.findElement(By.xpath("//*[@id='navlist']/li[3]/a"));
shangchengguanli.click();
//driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
Thread.sleep(4000);
WebElement shangchengdingdan=driver.findElement(By.xpath("//*[@id='navlist']/li[3]/ul/li[6]/a/span"));
int ok_size=driver.findElements(By.xpath("//*[@id='navlist']/li[3]/ul/li[6]/a/span")).size();
driver.findElements(By.xpath("//*[@id='navlist']/li[3]/ul/li[6]/a/span")).get(ok_size-1).click();
Thread.sleep(15000);
// ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
// driver.switchTo().window(tabs2.get(0));
WebElement tab_2=driver.findElement(By.xpath("//*[@id='iframe-wraper']/iframe"));
driver.switchTo().frame(1);
Thread.sleep(3000);
WebElement form= driver.findElement(By.xpath("//*[@id='orderTraceForm']"));
//订单号
WebElement dingdanhao=driver.findElement(By.xpath("//*[@id='ordSeq']"));
dingdanhao.sendKeys("201701180931380001");
WebElement chaxun=driver.findElement(By.xpath("//*[@id='serach']"));
chaxun.click();
Thread.sleep(3000);
//WebElement result1=driver.findElement(By.xpath("//*[@id='content']/tr/td[1]"));
WebElement result = new WebDriverWait(driver, 10).until(
ExpectedConditions.presenceOfElementLocated(
By.xpath("//*[@id='content']/tr/td[1]")
)
);
assertTrue(result.getText().contains("201701180931380001"));
dingdanhao.clear();
//用户机构
WebElement yonghujigou=driver.findElement(By.xpath("//*[@id='mainPreson']"));
yonghujigou.sendKeys("林");
chaxun.click();
Thread.sleep(3000);
WebElement result_org = new WebDriverWait(driver, 10).until(
ExpectedConditions.presenceOfElementLocated(
By.xpath("//*[@id='content']/tr/td[1]")
)
);
assertTrue(result_org.getText(),result_org.getText()!=null);
yonghujigou.clear();
//工作室名称
WebElement gongzuoshimingcheng=driver.findElement(By.xpath("//*[@id='stuName']"));
gongzuoshimingcheng.sendKeys("灰");
chaxun.click();
Thread.sleep(3000);
WebElement result_studio = new WebDriverWait(driver, 10).until(
ExpectedConditions.presenceOfElementLocated(
By.xpath("//*[@id='content']/tr/td[1]")
)
);
assertTrue(result_studio.getText(),result_studio.getText()!=null);
gongzuoshimingcheng.clear();
//用户手机号
WebElement yonghushoujihao=driver.findElement(By.xpath("//*[@id='username']"));
yonghushoujihao.sendKeys("15106061716");
chaxun.click();
Thread.sleep(3000);
WebElement result_cellphone = new WebDriverWait(driver, 10).until(
ExpectedConditions.presenceOfElementLocated(
By.xpath("//*[@id='content']/tr/td[1]")
)
);
assertTrue(result_cellphone.getText(),result_cellphone.getText()!=null);
yonghushoujihao.clear();
//订单状态
Select select_zhuangtai = new Select(driver.findElement(By.xpath("//*[@id='ordState']")));
for (int i=0;i<8;i++){
select_zhuangtai.selectByIndex(i);
System.out.println(select_zhuangtai.getOptions().get(i).getText()+"**********************************");
Thread.sleep(5000);
WebElement element = driver.findElement(By.xpath("//*[@id='serach']"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();
//chaxun.click();
Thread.sleep(10000);
}
}
}
复制代码
作者:
清晨一缕阳光
时间:
2018-3-27 08:32
学习了!
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2