2017-09-21 49 views
1

我打算通过在多个Web浏览器中使用硒来执行一些测试。到不同的网络驱动程序之间进行区分,我使用的代码下面的行:如何有效更改WebDriver对象的浏览器名称?

((RemoteWebDriver) driver).getCapabilities().getBrowserName(); 

这将返回一个字符串指示用于由driver对象的web浏览器。但是,对于我的Opera WebDriver对象,这会给我字符串'chrome'。我曾尝试使用DesiredCapabilities浏览器的名称明确地设置为“歌剧”改变这个:

DesiredCapabilities capabilities = new DesiredCapabilities(); 
capabilities.setBrowserName("opera"); 
WebDriver driver = new OperaDriver(capabilities); 

不幸的是,这并没有解决我的问题。如何有效更改网络浏览器名称?

+2

你不能这样做。它由驱动程序为特定浏览器设置,并且不能由您的代码写入。所以你需要一些其他的方式来存储这些信息,并回电 –

+0

我认为@TarunLalwani是正确的 – iamsankalp89

+0

我确实必须以另一种方式存储它。谢谢! –

回答

0

不幸的是,您将无法更改BrowserName。

什么,而不是你可以尝试是为专门处理多个浏览器创建功能: -

package multiBrowser; 
import org.testng.annotations.Test; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.firefox.FirefoxProfile; 
import org.openqa.selenium.firefox.internal.ProfilesIni; 
import org.openqa.selenium.ie.InternetExplorerDriver; 
import org.openqa.selenium.opera.OperaDriver; 
import org.testng.annotations.Parameters; 



public class MultiBrowserClass { 
WebDriver driver; 
@Test 
@Parameters("browser") 
public void multiBrowsers(String browserName) throws InterruptedException{ 
    if(browserName.equalsIgnoreCase("firefox")){ 
     System.setProperty("webdriver.firefox.marionette","D:\\My Work\\Setup\\JAR\\geckodriver.exe"); 
     ProfilesIni profile = new ProfilesIni();  
     FirefoxProfile myprofile = profile.getProfile("default"); 
     driver = new FirefoxDriver(myprofile); 

    } 
    if(browserName.equalsIgnoreCase("chrome")){ 

     System.setProperty("webdriver.chrome.driver", "D:\\My Work\\Setup\\JAR\\driver\\chromedriver.exe"); 
     driver = new ChromeDriver(); 
    } 
    else if(browserName.equalsIgnoreCase("IE")){ 

     System.setProperty("webdriver.ie.driver", "D:\\My Work\\Setup\\JAR\\driver\\IEDriverServer.exe"); 
     driver = new InternetExplorerDriver(); 
    } 
    else if(browserName.equalsIgnoreCase("opera")){ 

     System.setProperty("webdriver.opera.driver", "D:\\My Work\\Setup\\JAR\\driver\\operadriver.exe"); 
     driver = new OperaDriver(); 

    } 

    driver.manage().window().maximize(); 

    driver.navigate().to("https://"); 

    System.out.println(driver.getTitle()); 

    driver.findElement(By.xpath("//div[@id='navbar-main']/ul/li[5]/a")).click();  
    driver.findElement(By.xpath("//div[@id='navbar-main']/ul/li[5]/ul/li/a")).click(); 
    Thread.sleep(3000); 
    driver.findElement(By.name("email")).clear(); 
    driver.findElement(By.name("email")).sendKeys("[email protected]"); 
    driver.findElement(By.name("password")).clear(); 
    driver.findElement(By.name("password")).sendKeys("1qaz2wsx"); 
    Thread.sleep(3000); 
    driver.findElement(By.xpath("//form[@id='loginform']/div[8]/button")).click(); 
    Thread.sleep(5000); 

    if(driver.getPageSource().contains("Welcome [email protected]")){ 
     System.out.println("User Successfully logged in"); 

    }else{ 
     System.out.println("Username or password you entered is incorrect"); 
    } 

    driver.quit(); 

} 

} 

=======

+0

谢谢你的反应,但这不能回答我的问题。我已经具有初始化不同WebDriver对象的功能,我只是想在初始化后能够区分它们。 –

1

你的基本要求是,以确定浏览器的初始化,如果我没错,这可以通过获取用户代理使用JavascriptExecutor浏览器来完成如下:

String userAgent = (String) ((JavascriptExecutor) driver).executeScript("return navigator.userAgent;"); 

//following is for identifying opera browser initialization 
if(userAgent.contains("OPR/"){ 

    System.out.println("Browser currently in use is Opera"); 

} 

同样可以识别其他浏览器通过引用启动this链接

+0

这确实是我的要求。我已经通过在其他地方存储信息来解决问题,但出于好奇,测试了您的解决方案。它的工作原理,但我认为我现在的解决方案更适合。不过谢谢! –

相关问题