繁体   English   中英

从Selenium IDE生成的Java代码

[英]code Java generated from Selenium IDE

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

我确实测试了Selenium IDE,并在FireFox上生成了代码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的类:

公共类Config {私有静态属性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 \\ 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 / 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();
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM