Automated Browser Testing with Edge and Selenium in Python - GeeksforGeeks

24 Jan, 2021
Cross-browser testing is mandatory in the software industry. We all know that there are many browsers like Firefox, Chrome, Edge, Opera etc., are available. Rather than writing separate code to each and every browser, it is always a decent approach to go towards automated testing. Let us see how to do that using Selenium for Edge browser in Python. Here Edge WebDriver is used in order to run our Selenium automation test scripts over the Edge browser.
In order to perform Browser Automation using Edge And Selenium In Python, we need to carry out the following steps:
Edge browser: Microsoft Edge browser can be downloaded from https://www.microsoft.com/en-us/edge. If already installed, we can get the existing version of edge by using edge://version/
Selenium framework: Selenium is a powerful tool for controlling a web browser through the program. It is functional for all browsers, works on all major OS. It can be installed using the below command:
pip install selenium
Edge WebDriver: Selenium Edge Webdriver can be downloaded from this URL. Automatically testing will be getting done in Edge browser with this driver. Download the edge webdriver according to the operating system and unzip the file and get msedgedriver.exe .
Selenium tools for Microsoft Edge: This module has the required features for automated browser testing, it can be installed using the below command:
pip install msedge-selenium-tools selenium==3.141
Below is a program to execute a simple automated browser testing script:
Python3
# import required modulesfrom selenium import webdriver    # Driver Codeif __name__ == '__main__':      # create object    edgeBrowser = webdriver.Edge(r"msedgedriver.exe")      # open browser and navigate to facebook    edgeBrowser.get('https://www.facebook.com')
Output:
edge opening facebook page
On executing the script, we can see, edge browser has opened Facebook page as shown in the image. You can open any valid web page and automatically it will open the mentioned web page in edge browser.
Edge browser is opened.
It gets maximized.
It opens a web page(here www.lambdatest.com) .
It takes the user email as gfg@lambdatest.com .
Get the Submit button to click which is nothing but start free testing button in the form and it will navigate to next page with the URL as https://accounts.lambdatest.com/register?email=gfg@lambdatest.com .
If we want to know the id of each and every component of a web page, we can get it by means of View page source or by inspecting the appropriate section too.
Python3
# Import required modulefrom selenium.webdriver.opera.options import Optionsfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.common.exceptions import TimeoutExceptionfrom selenium import webdriverfrom time import sleep    # Driver Codeif __name__ == '__main__':          # Instantiate the webdriver with the executable location of MS Edge    # Provide the full location of the path to recognise correctly    edgeBrowser = webdriver.Edge(r"msedgedriver.exe")          # This is the step for maximizing browser window    edgeBrowser.maximize_window()          # Browser will get navigated to the given URL    edgeBrowser.get('https://www.lambdatest.com')    try:        # We need to insert Email in order to proceed further.         # So it is given by using 'useremail'        sampleElement = WebDriverWait(browser, 10).until(            EC.presence_of_element_located((By.ID, 'useremail')))                  # We can give a valid email address and since         # this page carries the email id alone, it just         # appends the email id at the end        sampleElement.send_keys("gfg@lambdatest.com")                  # A click is happening to move to next page        sampleElement.click()                  # A Submit button is searched to click and start         # free testing. Actually "testing_form" is the id         # of the form, which needs to get tested        sampleElement2 = WebDriverWait(browser, 10).until(            EC.element_to_be_clickable((By.CSS_SELECTOR,                                         "#testing_form > div")))                  # Starting free testing on LambdaTest        sampleElement2.click()                  # Just to show the set of actions happening, we can         # give sleep, U can change the values as per requirement        sleep(20)    except TimeoutException:        print("Trying to find the given element but unfortunately no element is found")    sleep(20)    # Once all operations over, we can close browser too    # edgeBrowser.close()
Output:
Automated browser testing is very convenient and as separate drivers are available for each browser, we can do the testing easily and no manual work is needed. Even automated testing is faster and can test multiple test pages very quickly and provide successful test results.
Picked
Python-selenium
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Defaultdict in Python
Create a directory in Python
Python | os.path.join() method
Python | Pandas dataframe.groupby()
Python | Get unique values from a list