2016-04-19 9 views
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メソッドを作ることができますか? 上記のコードにコメントを記入する作業方法。

助けが役に立つ記事、チュートリアル、分析のための同様のコードになります。

は、あなたがこのページファクトリを使用することができますアドバイス

答えて

1

をありがとう:

ログインクラスを

package locators; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.support.FindBy; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 

public class login { 

    @FindBy (id="p") 
    public WebElement password; 

    @FindBy (id="u") 
    public WebElement user; 

    public void Login(WebDriver driver, String login,String pass) { 
     WebDriverWait wait = new WebDriverWait(driver, 30); 
     wait.until(ExpectedConditions.elementToBeClickable(By.id("p"))); 
     password.clear(); 
     password.sendKeys(pass); 
     user.clear(); 
     user.sendKeys(login); 
     driver.findElement(By.id("submit_button")).click(); 
     }} 

テスト

import java.util.concurrent.TimeUnit; 
import org.testng.annotations.*; 
import static org.testng.Assert.*; 
import org.openqa.selenium.*; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.openqa.selenium.support.PageFactory; 

import locators.login; // if your test class and login class in the same package there is no need to import it 

public class Sortowanie{ 

private WebDriver driver; 
private String baseUrl; 
private StringBuffer verificationErrors = new StringBuffer(); 

@BeforeClass(alwaysRun = true) 
public void setUp() throws Exception { 
    driver = new ChromeDriver(); // for some reason FF don't want to load this page for me 
    baseUrl = "https://en-testwebapi.poczta-polska.pl/"; 
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
} 


@Test 
@Parameters({"login", "password"}) 
    public void testSortowanie(String login, String password) throws Exception { 
    login L = PageFactory.initElements(driver, login.class); // 
    driver.get(baseUrl); 
    L.Login(driver, login, password); 

} 

@AfterClass(alwaysRun = true) 
public void tearDown() throws Exception { 
    driver.quit(); 
    String verificationErrorString = verificationErrors.toString(); 
    if (!"".equals(verificationErrorString)) { 
    fail(verificationErrorString); 
} 
} 
} 

設定xmlファイル

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 

<suite name="Sortowanie Test Suite" verbose="2"> 

    <test name="Sortowanie Test 1" > 
     <parameter name="login" value="login 1"/> 
     <parameter name="password" value="pass 1"/> 
     <classes> 
     <class name="orders.test_setup"/> 
      <class name="Sortowanie"/> 
     </classes> 
    </test> 

    <test name="Sortowanie Test 2" > 
     <parameter name="login" value="login 2"/> 
     <parameter name="password" value="pass 2"/> 
     <classes> 
     <class name="orders.test_setup"/> 
      <class name="Sortowanie"/> 
     </classes> 
    </test> 

</suite> 
+0

あなたはconfigファイルについて何か言います。それは良いアイデアですが、私はそれのために使用する必要があります。 setup.propertiesを作成し、ログインして値を渡して、それをどのように使用できるか? –

+0

コードは私のためには機能しませんが、設定ファイルについてのsugestionは本当に役に立ちました。 –

+0

設定ファイルを実行するように自分のコードを修正しました。 TestNGスイートとしてxmlファイルを実行する必要があります。また、 'import org.openqa.selenium.chrome.ChromeDriver;'について忘れてしまったので、私のコードは動作しませんでした。 – Angusiasty

関連する問題