2016-04-19 13 views
0

Ich möchte die Login-Methode verwenden, um bei jedem Test den gleichen Code zu wiederholen. Ich überprüfe ähnliches Thema hier und konnte keine Lösung finden oder es einfach nicht bekommen.So konfigurieren Sie die Login-Methode, um bei jedem Test doppelten Code zu vermeiden [selen] [java]

Test wird von Selenium IDE automatisch generiert, so habe ich mehr Frage über eine Methode dort. Ich werde Dinge markieren, die mich verwirren. Sein mein Test und Verfahren, die ich gemacht:

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; 
    } 
} 
} 

So zum Wohle des Friedens ich meine Fragen wiederholen: Wie kann ich Login-Methode machen, die params Wert zu erhalten und wo ich es brauchen zu implementieren? Wie Arbeitsmethode, die ich in Kommentar auf Code oben markieren.

Jede Hilfe wird nützlicher Artikel, Tutorials, ähnlicher Code für die Analyse sein.

Vielen Dank für Beratung

Antwort

1

Sie Seite Fabrik für diese verwenden:

Login-Klasse

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(); 
     }} 

Ihre Test

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); 
} 
} 
} 

Config XML-Datei

<?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

Sie sagen etwas über die Konfigurationsdatei. Es wird eine bessere Idee sein, aber was ich dafür brauche. Create setup.properties setzen Login und übergeben Wert und wie kann ich das verwenden? –

+0

Code funktioniert nicht für mich, aber Sie suggestion über die Konfigurationsdatei war wirklich hilfreich. –

+0

Ich habe meinen Code so geändert, dass er jetzt auf der Konfigurationsdatei läuft. Sie müssen eine XML-Datei als TestNG-Suite ausführen. Auch mein Code funktionierte nicht, weil ich "import.org.openqa.selenium.chrome.ChromeDriver;" vergessen hatte. – Angusiasty

Verwandte Themen