代码Java从Selenium IDE生成

时间:2015-04-03 12:32:03

标签: java selenium-webdriver selenium-ide

我正在测试不同浏览器(FireFox,Chrome,IE)上的应用程序。

我测试selenium IDE并且我生成了代码java.on FireFox我每次更新生成的代码都会更新(id,cddSelector,Xpath ...)因为它不能正常工作。此外,当我在Chrome上进行同样的测试,它不起作用,我必须第二次更改代码。

我有50到60个测试用例,并且使用这种方法非常困难。

请您知道为所有浏览器使用相同的代码java吗?

在一个类的代码下面:

{
public class ConnexionMotDePasseErroneCommunFront {

private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
private String navigateur = "";
private String versionChrome="";


@Before
public void setUp() throws Exception {

    baseUrl = Config.URLMULTISHIPING;

    navigateur = Config.NAVIGATEUR;

    switch (navigateur) {
    case "firefox":
        driver = new FirefoxDriver();

        break;

    case "chrome":

        versionChrome=Config.VERSIONCHROMEDRIVER;

        System.setProperty("webdriver.chrome.driver",new String("C:\\dev\\drivers\\ChromeDriver\\").concat(versionChrome).concat("\\chromedriver.exe"));


        driver = new ChromeDriver();

        break;

    case "ie":

        System.setProperty("webdriver.ie.driver",
                "C:\\dev\\drivers\\IeDriver\\IEDriverServer.exe");

        DesiredCapabilities sCaps = DesiredCapabilities.internetExplorer();
        sCaps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
        sCaps.setJavascriptEnabled(true);
        driver = new InternetExplorerDriver(sCaps);

        //driver = new InternetExplorerDriver();


    case "opera":
        driver = new OperaDriver();

        break;

    case "safari":
        driver = new SafariDriver();

        break;

    default:
        throw new RuntimeException("Browser type unsupported");

    }


    driver.manage().timeouts()
            .implicitlyWait(Config.TIMEOUT, TimeUnit.SECONDS);

}

@Test
public void testConnexionMotDePasseErroneCommunFront() throws Exception {
    driver.get(baseUrl + "/");
    driver.findElement(By.id("mini-login")).click();
    driver.findElement(By.id("mini-login")).clear();
    driver.findElement(By.id("mini-login")).sendKeys("recette15@yahoo.fr");
    driver.findElement(By.id("mini-password")).click();
    driver.findElement(By.id("mini-password")).clear();
    driver.findElement(By.id("mini-password")).sendKeys("123456");
    driver.findElement(By.xpath("//button[@type='submit']")).click();
    driver.findElement(
            By.xpath("//a[@href='http://10.1.0.142:8081/customer/account/forgotpassword/']"))
            .click();
    driver.findElement(By.id("email_address")).sendKeys(
            "recette15@yahoo.fr");
    driver.findElement(By.xpath("//button[@type='submit']")).click();

    driver.findElement(
            By.xpath("//a[@href='http://10.1.0.142:8081/customer/account/login/']"))
            .click();
}

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

private boolean isElementPresent(By by) {
    try {
        driver.findElement(by);
        return true;
    } catch (NoSuchElementException e) {
        return false;
    }
}

private boolean isAlertPresent() {
    try {
        driver.switchTo().alert();
        return true;
    } catch (NoAlertPresentException e) {
        return false;
    }
}

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

这是Config的类:

public class Config {     private static Properties config;

static {
    if (config == null) {
        config = new Properties();
        try {
            config.load(new FileInputStream("src/test/resources/config.properties"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public static final String NAVIGATEUR = config.getProperty("navigateur");
public static final String URLONEPAGE = config.getProperty("urlOnePage");
public static final String URLMULTISHIPING = config.getProperty("urlMultiShiping");
public static final String URLDEMO = config.getProperty("urlDEMO");
public static final Integer TIMEOUT = Integer.valueOf(config.getProperty("timeout"));
public static final String USER = config.getProperty("usernameBD");
public static final String PASSWORD=config.getProperty("passwordBD");
public static final String JDBC_DRIVER =config.getProperty("driver");
public static final String DB_URL=config.getProperty("cheminBd");
public static final String REQUEST=config.getProperty("requete");
public static final String EMAIL=config.getProperty("email");
public static final String PASS=config.getProperty("pass");
public static final String PASSNEW=config.getProperty("passNew");
public static final String ADRESSEEMAILOK=config.getProperty("adresseEmailOK");
public static final String PASSERR=config.getProperty("userPassErr");
public static final String EMAILDONNATEUR=config.getProperty("emailDonnateur");
public static final String EMAILADMIN=config.getProperty("emailAdmin");
public static final String PASSADMIN=config.getProperty("passAdmin");
public static final String NUMERO_CARTE_BANCAIRE=config.getProperty("numeroCarteBancaire");
public static final String TRIGRAMME=config.getProperty("triGramme");
public static final String PASSESPACEDONATEUR=config.getProperty("passEspaceDonateur");
public static final String STREET_1=config.getProperty("street_1");
public static final String STREET_1_NEW=config.getProperty("street_1_new");
public static final String CODEPOSTALE=config.getProperty("codePostale");
public static final String TEL=config.getProperty("tel");
public static final String VERSIONCHROMEDRIVER=config.getProperty("versionChromeDriver");

}

请告诉我IE的这种配置是否正确? :

System.setProperty( “webdriver.ie.driver”,                     “C:\ dev的\驱动\ IeDriver \ IEDriverServer.exe”);

        DesiredCapabilities sCaps = DesiredCapabilities.internetExplorer();
        sCaps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
        sCaps.setJavascriptEnabled(true);
        driver = new InternetExplorerDriver(sCaps);

先谢谢

最诚挚的问候,

1 个答案:

答案 0 :(得分:0)

你在这里。您可以更改变量"浏览器"在不同的浏览器中运行。 此外,您必须在项目中添加" driver / chromedriver.exe" " driver / IEDriverServer.exe" 夹。

@Test将包含您的测试。你可以创建新的@Test方法,你需要多少。

package main;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class DiffBrowsers {

    public WebDriver driver;

    @Before
    public void setUp() {

        int browser = 1; // 1 = Firefox, 2 = Chrome, 3 = IE

            if(browser == 1) {
                driver = new FirefoxDriver();
            }
            else if (browser == 2) {
                System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
                driver = new ChromeDriver();
            }
            else {
                System.setProperty("webdriver.ie.driver", "driver/IEDriverServer.exe");
                driver = new InternetExplorerDriver();
            }
    }

    @Test
    public void myTest() {
        driver.get("http://google.com/");
    }

    @After
    public void tearDown() {
        driver.quit();
    }

}