Executing Tests in Multiple Browsers



Selenium supports multiple browsers like Chrome, Firefox, Safari, IE, and so on. For running the tests in a particular browser we should have to download the executable file for that browser from the below link −

https://www.selenium.dev/downloads/

Once the link is launched, scroll down to the Browsers section. Under this, all the available browsers which support execution are listed. Click on the documentation link to download the corresponding executable file.

Browsers

For example, to trigger the tests on Chrome, click on the documentation link. In the next page, the list of all the versions of chromedriver shall be available.

List of All Versions

Click on a link to download the chromedriver.exe file which matches with our local Chrome browser version. On the following page, we shall be directed to the zip files available for download for the platforms Windows, Linux, and Mac.

Chromedriver exe

Click on a link to download the chromedriver.exe file which matches with our local operating system. Once the download is done, unzip the file and save it within the project directory.

For example in our project, we have saved the chromedriver.exe file within the drivers folder. Then we have to specify the path of this file within the webdriver.Chrome(executable_path='< path of chromedriver >').

Chrome Driver

Code Implementation

The code implementation for supporting multiple browsers is as follows −

from selenium import webdriver
driver = webdriver.Chrome(executable_path='../drivers/chromedriver')
#implicit wait time
driver.implicitly_wait(5)
#url launch
driver.get("https://www.tutorialspoint.com/index.htm")
#get browse name
l = driver.capabilities['browserName']
print('Browser name: ' + l)
#driver quit
driver.quit()

Output

Python Test

The output shows the message - Process with exit code 0 meaning that the above Python code executed successfully. Also, the browser in which the test has executed - chrome gets printed in the console.

Similarly, if we want to execute the test in the Firefox browser (versions greater than 47), we have to use the geckodriver.exe file.

Advertisements