In the previous section, we have discussed about the Locators handler in Selenium with Python. We have launched Chrome Driver with executable file chromedriver.exe. Now we will discuss about the Cross Browser Testing.
- Run Test case in multiple browsers
Let’s think a situation of cross browser testing where, you need to work with different browsers like Chrome, Firefox etc. multiple times. In this situation, you need to download separate driver for Chrome, Firefox and import them and execute your script.
Is this a good option?
Obviously No. In this way you may loose lots of time and resource. For this you need to use Webdriver Manager. You do not need to download any driver for your browser to automate. Webdrievr manager will download them automatically depending on your browsers version. If your Browser will update in future also, it will download the driver with correct version depending your version of the browsers. It will check which operating system you are using and which version of browsers you are using. If you do not have the corresponding exe file, then it will automatically download on the basis of the browser version and OS.
Let’s discuss this with and Example:
- Let’s create a variable for Browser name – browserName. Basis on this variable we will write the if condition to open the browser based on the variable.
browserName = "Chrome"
- Next step is to write the code to open the browser. For this Earlier we were writing webdriver.Chrome() and were passing the chrome driver location as parameter. Now we will use Webdriver Managers. Webdriver manager library is not part of the Selenium. You need to download explicitly and import to your application.
To download, search webdriver manager in google. Or you can directly navigate to https://pypi.org/project/webdriver-manager/ link to download webdriver manager.
You need to copy the command marked in above image. And run on your terminal.
Now you have successfully installed webdriver manager libraries to you application and you are open to use the same.
if browserName == 'Chrome': driver = webdriver.Chrome(ChromeDriverManager().install()) elif browserName == 'Firefox': driver = webdriver.firefox(executable_path = GeckoDriverManager().install()) elif browserName = "Safari": driver = webdriver.safari()
It will automatically download the executable driver file and handle implicitly. You can notice that there is some syntax difference in Chrome and Firefox driver. For safari there is not webdriver manager exist.
from selenium import webdriver from selenium.webdriver.common.by import By from webdriver_manager.chrome import ChromeDriverManager from webdriver_manager.firefox import GeckoDriverManager import time browserName = "Firefox" if browserName == 'Chrome': driver = webdriver.Chrome(ChromeDriverManager().install()) elif browserName == 'Firefox': driver = webdriver.Firefox(executable_path=GeckoDriverManager().install()) elif browserName == 'Safari': driver = webdriver.Safari() else: print("Enter Correct Browser Name:" + browserName ) driver.get("https://www.facebook.com/") print(driver.title) driver.find_element(By.ID, 'email').send_keys("email@example.com") driver.find_element(By.ID, 'pass').send_keys("xxxxxxxx") driver.find_element_by_name("login").click()
In this section, we have discussed about the cross browsers testing. In next section, we will learn about handling dropdown in Selenium Python.