Selenium
のコードの一部を別のプロジェクトに移動したいのですが、何度もこのテストを何度も使用する予定です。Selenium他のファイルから 'code'を呼び出す方法(Java)
私の場合のほとんどは管理者としてサービスにログインする必要があるため、すべてのテストでこのコードを使用する必要はありません。
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
を使用しようとしていたが、私は、変数を持って、私は1つのより多くの時間をdeficeしたくない、と正直に私はできません
例えばdriver = new ChromeDriver(options);
ブラウザの新しいインスタンスを開きます。私はこれを望んでいません。
は、たぶん私は、私は私はあなたがスイートを持っている必要がありますね。この
Test1.
Common.loginAsAdmin(driver); //this is call to another class in another project
Rest of Code (deleting users)
Test2.
Common.loginAsAdmin(driver); //this is call to another class in another project
Rest of Code (creating users)
Test3.
Common.loginAsAdmin(driver); //this is call to another class in another project
Rest of Code (modify users)
(...)
申し訳ありませんが、しかし、私は正確に何をしたい理解できませんでした。あなたのタイトルは、別のプロジェクトからクラスを呼びたいと言っています。テキストにはコードを複製し、ページオブジェクトに関連するものをミックスするとします。何か不足していますか? – Tom
コードを書式設定してください。この種の書式設定で何が起こっているのかを知るのはかなり難しいです。 –
これは私の解説です。一般的に、すべてのテストで同じコードを繰り返す必要はないので、他のファイルから呼び出す場合は、今後の変更が簡単になります – Krzysztof