Commit cbaa8cf7 authored by 谢卓城's avatar 谢卓城

feat: 1.新增分享测试用例。2.edge浏览器配置。

parent 88b071ea
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import java.util.List;
public class SeleniumTest {
EdgeDriver driver;
public static void main(String[] args) {
SeleniumTest test = new SeleniumTest();
test.login();
test.testShare();
}
public void testShare(){
WebElement div = getWaitWebElement(By.cssSelector("div[class='item']:nth-child(4)"));
div.click();
WebElement cell = getWaitWebElement(By.cssSelector("div[class='con-item bg-f']:first-child"));
cell.click();
WebElement shareBtn = getWaitWebElement(By.cssSelector("div[class='sign-details']"));
shareBtn.click();
WebElement shareCell = getWaitWebElement(By.cssSelector("div[class='share-cell']:first-child"));
String innerText = shareCell.getAttribute("innerText");
assert innerText != "";
if (innerText != "微信") {
System.out.println("=============不是业主群");
driver.close();
}
}
public WebElement getWaitWebElement(By by) {
// 选择角色
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10), Duration.ofSeconds(1));
return wait.until(new ExpectedCondition<WebElement>() {
public WebElement apply(WebDriver d) {
return d.findElement(by);
}
});
}
public void login(){
//设置驱动
System.setProperty("webdriver.edge.driver", "driver/msedgedriver");
System.setProperty("webdriver.chrome.verboseLogging", "true");
//在当前浏览器打开
// EdgeOptions options = new EdgeOptions();
// options.setExperimentalOption("debuggerAddress", "127.0.0.1:9222");
//创建驱动
driver = new EdgeDriver();
//与将要测试的网站建立连接
driver.get("https://dev-h5.smart-gani.com/#/login");
// 强转js执行对象
JavascriptExecutor javaScript = (JavascriptExecutor) driver;
List<WebElement> inputs = driver.findElements(By.className("van-field__control"));
String[] infos = new String[]{"13366660001","123456"};
for (int i = 0; i < inputs.size(); i++) {
WebElement input = inputs.get(i);
javaScript.executeScript("arguments[0].value = '';", input);
input.sendKeys(infos[i]);
}
WebElement loginBtn = driver.findElement(By.cssSelector("button[type=submit]"));
loginBtn.click();
// 选择角色
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10), Duration.ofSeconds(1));
WebElement cell = wait.until(new ExpectedCondition<WebElement>() {
public WebElement apply(WebDriver d) {
return d.findElement(By.cssSelector("div[class='pation blue']:nth-child(1)"));
}
});
cell.click();
}
}
\ No newline at end of file
...@@ -3,24 +3,23 @@ import org.openqa.selenium.By; ...@@ -3,24 +3,23 @@ import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement; import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration; import java.time.Duration;
import java.util.List; import java.util.List;
//import org.testng.Assert; import org.testng.Assert;
import org.testng.asserts.SoftAssert;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.testng.annotations.AfterTest; import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
public class Chromedriver { public class Chromedriver {
WebDriver driver; ChromeDriver driver;
SoftAssert assertion;
public static void main(String[] args) { public static void main(String[] args) {
Chromedriver chromedriver = new Chromedriver();
chromedriver.login();
// 1)Class选择:driver.findElement(By.className("s_ipt")); // 1)Class选择:driver.findElement(By.className("s_ipt"));
// WebElement element=driver.findElement(By.className("s_btn")); // WebElement element=driver.findElement(By.className("s_btn"));
...@@ -61,22 +60,45 @@ public class Chromedriver { ...@@ -61,22 +60,45 @@ public class Chromedriver {
// } // }
} }
@Test public WebElement getWaitWebElement(By by) {
public void login() // 选择角色
{ WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10), Duration.ofSeconds(1));
System.out.println("===========login"); return wait.until(new ExpectedCondition<WebElement>() {
public WebElement apply(WebDriver d) {
return d.findElement(by);
}
});
}
public void setChromeDriver() {
//设置驱动 //设置驱动
System.setProperty("webdriver.chrome.driver", "driver/chromedriver"); System.setProperty("webdriver.chrome.driver", "driver/chromedriver");
//在当前浏览器打开 //在当前浏览器打开
ChromeOptions options = new ChromeOptions(); //ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("debuggerAddress", "127.0.0.1:9222"); //options.setExperimentalOption("debuggerAddress", "127.0.0.1:9222");
//创建驱动 //创建驱动
driver = new ChromeDriver(options); driver = new ChromeDriver();
//与将要测试的网站建立连接 //与将要测试的网站建立连接
driver.get("https://dev-h5.smart-gani.com/#/login"); driver.get("https://dev-h5.smart-gani.com/#/login");
// Assert.assertNotNull(driver,"驱动创建失败!"); assertion = new SoftAssert();
Assert.assertNotNull(driver,"驱动创建失败!");
}
@BeforeTest
public void beforeLogin() {
System.out.println("===========beforeLogin");
setChromeDriver();
}
@Test
public void login()
{
System.out.println("===========login");
// 强转js执行对象 // 强转js执行对象
JavascriptExecutor javaScript = (JavascriptExecutor) driver; JavascriptExecutor javaScript = (JavascriptExecutor) driver;
...@@ -89,37 +111,37 @@ public class Chromedriver { ...@@ -89,37 +111,37 @@ public class Chromedriver {
input.sendKeys(infos[i]); input.sendKeys(infos[i]);
} }
WebElement loginBtn = driver.findElement(By.cssSelector("button[type=submit]")); WebElement loginBtn = driver.findElement(By.cssSelector("button[type=submit]"));
// Assert.assertNotNull(loginBtn,"登录按钮获取失败"); Assert.assertNotNull(loginBtn,"登录按钮获取失败");
loginBtn.click(); loginBtn.click();
System.out.println("===========选择角色"); WebElement cell = getWaitWebElement(By.cssSelector("div[class='pation blue']:nth-child(1)"));
Assert.assertNotNull(cell);
// WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10), Duration.ofSeconds(1));
// WebElement cell = wait.until(new ExpectedCondition<WebElement>() {
// public WebElement apply(WebDriver d) {
// return d.findElement(By.cssSelector("div[class='pation blue']:nth-child(1)"));
// }
// });
// Assert.assertNotNull(cell);
// cell.click(); cell.click();
// System.out.println(cell.getAttribute("outerHTML"));
} }
@AfterTest @AfterTest
public void afterLogin() { public void afterLogin() {
System.out.println("===========afterLogin"); System.out.println("===========afterLogin");
testShare(); testShare();
} }
public void testShare() { public void testShare() {
WebElement div = getWaitWebElement(By.cssSelector("div[class='item']:nth-child(4)"));
div.click();
WebElement cell = getWaitWebElement(By.cssSelector("div[class='con-item bg-f']:first-child"));
cell.click();
// driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(1)); WebElement shareBtn = getWaitWebElement(By.cssSelector("div[class='sign-details']"));
shareBtn.click();
WebElement element = driver.findElement(By.cssSelector("div[class='con-items']")); WebElement shareCell = getWaitWebElement(By.cssSelector("div[class='share-cell']:first-child"));
// Assert.assertNotNull(element,"元素获取失败"); String innerText = shareCell.getAttribute("innerText");
element.click(); assertion.assertEquals(innerText,"业主","不是业主微信群");
driver.close();
assertion.assertAll();
} }
} }
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