Tutorial 11- Create and run first selenium script in Behave BDD
Welcome to the 11th article in Behave BDD series!
What you will Learn:
Download Chrome driver executable
Create and run first selenium script using behave in chrome
Install latest Firefox browser
Download GeckoDriver for firefox browser
Run the script in firefox browser
Download chrome driver executable
Navigate to official website of selenium https://www.selenium.dev/downloads/
Expand the ‘Browsers’ section, see below
Click ‘documentation’ link under ‘Chrome’ sub section, the below page comes up https://sites.google.com/a/chromium.org/chromedriver/
Click ‘Downloads’ that you see above, the below page comes up https://sites.google.com/a/chromium.org/chromedriver/downloads
Look for the chrome version your machine (as you can see below, the version is 90)
Since chrome version is 90, let us click ‘ChromeDriver 90.0.4430.24’ link
The below page comes up https://chromedriver.storage.googleapis.com/index.html?path=90.0.4430.24/
Click and download ‘chromedriver_win32.zip’ > extract it > place it in the location C:\Users\....\AppData\Local\Programs\Python\Python37-32\Scripts
Make sure to add this path in environment variables (you can refer Tutorial_2_Setup_Python_Pip for details)
Create and run first selenium script using behave in chrome
Ensure that selenium is installed in your system (you can refer Tutorial_3_Setup_PythonVirtualEnv_Selenium_Behave for details).
Open command prompt and cd to C:\Users\....\AppData\Local\Programs\Python\Python37-32\Scripts
Execute below command. You should see selenium along with its version in the result
Create and run first selenium script using behave
We will first create a simple feature file
The steps are self-explanatory. Let us inspect the logo and find out its xpath
So the xpath is //img[@alt='Logo']
Let us now create the corresponding step definition file. The functions are self-explanatory (in line#7 we are launching browser, in line#11 we are navigating to website, in line#15 we are capturing the status of logo, in line#16 we are asserting if the logo got displayed or not, line#17 ensures that the chrome browser does not close immediately)
The advantage of keeping the browser drivers in the location C:\Users\....\AppData\Local\Programs\Python\Python37-32\Scripts
is that we don’t have to explicitly mention the driver location path in line number 7 viz webdriver.Chrome("C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\Scripts")
Let us now run the feature
Note: The first time when you run, you might get a windows access popup, just allow access
Notice that the feature passed.
Let us intentionally make the xpath incorrect
Save the file
Re-run the feature. Notice that we get an exception and 3rd step fails
Let us change back the xpath
Install latest Firefox browser
Download latest firefox browser
Download GeckoDriver for firefox browser
To work with firefox browsers, we need to download geckodriver. To do that, go to https://github.com/mozilla/geckodriver/releases
Download and place it in below location
Run the script in firefox browser
Comment line number 7 and add line number 8
Run the feature file
The script passes. Also, the firefox browser does not close automatically.
So this is how we integrate selenium with behave and execute the scripts.
Thank you for reading!