0
私はすべてのテストを同じコードを繰り返してログインメソッドintedを使用したいと思います。私はここで似たような話題をチェックし、解決策を見つけることができなかった、あるいはそれを得られなかった。どのようにコードを重複しないようにログインメソッドを設定する[セレン] [Java]
テストはSelenium IDEによって自動生成されるので、そこにいくつかのメソッドについて質問があります。私は私を混乱させるものに印をつけます。 その私が作った私のテストおよび方法:私は私の質問を繰り返し、平和のためにそう
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.*;
import static org.testng.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Sortowanie{
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
String[] params;
@BeforeClass(alwaysRun = true)
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "https://en-testwebapi.poczta-polska.pl/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testSortowanie() throws Exception {
driver.get(baseUrl + "/");
login();
driver.findElement(By.xpath("(//input[@type='search'])[2]")).clear();
driver.findElement(By.xpath("(//input[@type='search'])[2]")).sendKeys("1");
driver.findElement(By.xpath("(//input[@type='search'])[3]")).clear();
driver.findElement(By.xpath("(//input[@type='search'])[3]")).sendKeys("09");
driver.findElement(By.xpath("(//input[@type='search'])[3]")).clear();
driver.findElement(By.xpath("(//input[@type='search'])[3]")).sendKeys("");
driver.findElement(By.xpath("(//input[@type='search'])[4]")).clear();
driver.findElement(By.xpath("(//input[@type='search'])[4]")).sendKeys("21");
driver.findElement(By.xpath("(//input[@type='search'])[4]")).clear();
driver.findElement(By.xpath("(//input[@type='search'])[4]")).sendKeys("");
}
@AfterClass(alwaysRun = true)
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
//I want to use params which will be defined in run configuration
public void login(){
driver.findElement(By.id("p")).clear();
driver.findElement(By.id("p")).sendKeys(params[1]);
driver.findElement(By.id("u")).clear();
driver.findElement(By.id("u")).sendKeys(params[0]);
driver.findElement(By.id("submit_button")).click();
}
//Where and when i need to use this method ??
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
//Where and when i need to use this method ??
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
//Where and when i need to use this method ??
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
: はどのようにparams値を取得し、私はそれを実装する場所を必要とloginメソッドを作ることができますか? 上記のコードにコメントを記入する作業方法。
助けが役に立つ記事、チュートリアル、分析のための同様のコードになります。
は、あなたがこのページファクトリを使用することができますアドバイス
あなたはconfigファイルについて何か言います。それは良いアイデアですが、私はそれのために使用する必要があります。 setup.propertiesを作成し、ログインして値を渡して、それをどのように使用できるか? –
コードは私のためには機能しませんが、設定ファイルについてのsugestionは本当に役に立ちました。 –
設定ファイルを実行するように自分のコードを修正しました。 TestNGスイートとしてxmlファイルを実行する必要があります。また、 'import org.openqa.selenium.chrome.ChromeDriver;'について忘れてしまったので、私のコードは動作しませんでした。 – Angusiasty