2015-04-03 123 views
0

我正在测试不同浏览器(FireFox,Chrome,IE)上的应用程序。代码从Selenium IDE生成的代码

我测试硒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("[email protected]"); 
    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(
      "[email protected]"); 
    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; 
    } 
} 
} 
} 

,这是类配置的:

公共类配置{ 私有静态属性配置;

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

由于提前

最好的问候,

+0

嗨LittlePanda, – 2015-04-03 15:38:16

+0

请问,你可以给我你的意见关于我的修改和加入的代码吗? – 2015-04-03 15:39:04

回答

0

给你。您可以将变量“browser”更改为在不同的浏览器中运行。 你也必须把“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(); 
    } 

} 
+0

非常感谢你。在我的例子中,我使用了setUp()和teardown()+测试用例...请问,我能做些什么来使你的代码适合我的代码。非常感谢你。 – 2015-04-07 09:49:07

+0

嗨,我已经编辑了答案。我希望它能帮助你。 – vdmytsyk 2015-04-07 10:48:10