簡體   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