Selenium Python webdriver condition
Question:
My script can give me 3 separate webdrivers based on what I want i.e., my script can either install the chromedriver or safaridriver, or firefoxdriver. As an example:
-
chromedriver
:
<selenium.webdriver.chrome.webdriver.WebDriver (session="8032fa9146e8fc2a764aa278a1521014")>
-
safaridriver
:
<selenium.webdriver.safari.webdriver.WebDriver (session="7703B595-D074-40C4-82D5-D4A265C2AAB2")>
-
firefoxdriver
:
<selenium.webdriver.firefox.webdriver.WebDriver (session="d2549d48-bec3-4ccc-b95f-7339bbe4ca60")>
What is the best way to have the script run differently based on browser?
Answers:
Using Selenium Java clients and a framework like TestNG you can use the @Parameters
tag to choose the browser of your choice through testng.xml as follows:
private WebDriver driver;
@Parameters ({"browser"})
@BeforeTest
public void preCondition(String browser)
{
try
{
if(browser.equalsIgnoreCase("Firefox"))
{
driver = new FirefoxDriver();
}
if(browser.equalsIgnoreCase("Chrome"))
{
driver = new ChromeDriver();
}
if(browser.equalsIgnoreCase("IE"))
{
driver = new InternetExplorerDriver();
}
}
catch (WebDriverException e)
{
System.out.println("Browser not found" +e.getMessage());
}
}
This approach will be the best approach to download, create and use any type of WebDriver
My script can give me 3 separate webdrivers based on what I want i.e., my script can either install the chromedriver or safaridriver, or firefoxdriver. As an example:
-
chromedriver
:<selenium.webdriver.chrome.webdriver.WebDriver (session="8032fa9146e8fc2a764aa278a1521014")>
-
safaridriver
:<selenium.webdriver.safari.webdriver.WebDriver (session="7703B595-D074-40C4-82D5-D4A265C2AAB2")>
-
firefoxdriver
:<selenium.webdriver.firefox.webdriver.WebDriver (session="d2549d48-bec3-4ccc-b95f-7339bbe4ca60")>
What is the best way to have the script run differently based on browser?
Using Selenium Java clients and a framework like TestNG you can use the @Parameters
tag to choose the browser of your choice through testng.xml as follows:
private WebDriver driver;
@Parameters ({"browser"})
@BeforeTest
public void preCondition(String browser)
{
try
{
if(browser.equalsIgnoreCase("Firefox"))
{
driver = new FirefoxDriver();
}
if(browser.equalsIgnoreCase("Chrome"))
{
driver = new ChromeDriver();
}
if(browser.equalsIgnoreCase("IE"))
{
driver = new InternetExplorerDriver();
}
}
catch (WebDriverException e)
{
System.out.println("Browser not found" +e.getMessage());
}
}
This approach will be the best approach to download, create and use any type of WebDriver