我想将 Alamofire.request(Posturl, method: .post, parameters: prodArray, encoding: URLEncoding.httpBody, headers: headers)
.responseData{ response in
let success = json["status"]["type"].boolValue
if success == true {
}
}
中我的部分代码移到另一个项目中,因为我打算在很多测试中多次使用它。
一开始,我的大部分情况都需要以管理员身份登录服务,因此我不想在每次测试中都使用此代码。
Selenium
我正在尝试使用public void tc_mp_011() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 10);
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
long start = System.currentTimeMillis();
long end = start + 10 * 1000;
while (true) {
if (driver.findElement(By.xpath("//*[@id='signout']/div/div/div/span")).isDisplayed()) {
Thread.sleep(10);
if (System.currentTimeMillis() < end);
else {
System.err.println("timeout after: " + (System.currentTimeMillis() - start) + "ms");
driver.navigate().refresh();
start = System.currentTimeMillis();
end = start + 10 * 1000;
}
continue;
} else {
System.out.println("login Page loaded after " + (System.currentTimeMillis() - start) + " ms");
break;
}
}
wait.until(ExpectedConditions.elementToBeClickable(By.id("username")));
start = System.currentTimeMillis();
end = start + 10 * 1000;
driver.findElement(By.id("username")).click();
WebElement a = driver.findElement(By.id("username"));
a.sendKeys("admin");
wait.until(ExpectedConditions.elementToBeClickable(By.id("password")));
driver.findElement(By.id("password")).click();
WebElement b = driver.findElement(By.id("password"));
b.sendKeys("admin");
if (driver.findElement(By.id("username")).getAttribute("value").equals("admin") && driver.findElement(By.id("password")).getAttribute("value").equals("admin")) {
} else {
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("username")).sendKeys("admin");
driver.findElement(By.id("password")).sendKeys("admin");
}
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.btn.btn-primary")));
driver.findElement(By.cssSelector("button.btn.btn-primary")).click();
//step 1
start = System.currentTimeMillis();
end = start + 10 * 1000;
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#panel > div.panel-body > div.col-md-12 > div.controls > #enable")));
if (System.currentTimeMillis() < end) {
try {
while (
driver.findElement(By.cssSelector("#panel > div.panel-body > div.col-md-12 > div.controls > #enable")).isDisplayed()) {
System.out.println("Pageloaded after " + (System.currentTimeMillis() - start) + " ms");
break;
}
} catch (Exception nse) {
}
} else {
System.err.println("Fail - Page not loaded in 10s");
org.testng.Assert.fail("Fail - Page not loaded in given time");
}
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#panel > div.panel-body > div.col-md-12 > div.controls > #enable")));
WebElement username = driver.findElement(By.id("signedinusername"));
wait.until(ExpectedConditions.textToBePresentInElement(username, "admin"));
if (driver.findElement(By.id("signedinusername")).getText().equals("admin")) {} else {
org.testng.Assert.fail("Fail - login as admin");
}
driver.findElement(By.linkText("Sign Out")).click();
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
driver.get("http://192.168.1.100./");
}
@AfterMethod
public void afterMethod() {
// Close the driver
driver.quit();
}
,但我有变量,我不想再次定义,老实说我不能
例如Page Object Pattern
将打开浏览器的新实例,我不希望这样。
也许有一种不同的简单方法来处理这个
我不想复制代码 我想做这样的事情
driver = new ChromeDriver(options);
答案 0 :(得分:0)
我想你应该有一套测试,通常每个测试都有一个方法,以防你使用Junit,试试这个
@Test
public void testOne() { //all your Selenium code here}
@Test
public void testTwo() { //all your Selenium code here}
最后,如果你需要那些之间的对话,请尝试使用范围类变量
答案 1 :(得分:0)
你的代码真的搞砸了,你应该清理它。如果您正在谈论的代码可重用性是创建您需要调用的函数,然后在测试中调用它们。不要在测试中写下所有内容。说几个问题
我建议谷歌页面对象模式并学习如何使用它。
但这将是一个好的开始 https://sqa.stackexchange.com/questions/9901/how-to-implement-page-object-and-page-factory-pattern-in-selenium-webdriver