📜  在Python中使用 Edge 和Selenium进行自动浏览器测试

📅  最后修改于: 2022-05-13 01:55:47.422000             🧑  作者: Mango

在Python中使用 Edge 和Selenium进行自动浏览器测试

跨浏览器测试在软件行业是强制性的。我们都知道有很多浏览器,如 Firefox、Chrome、Edge、Opera 等,都可以使用。与为每个浏览器编写单独的代码相比,它始终是实现自动化测试的一种不错的方法。让我们看看如何在Python中使用Selenium for Edge 浏览器来做到这一点。这里使用 Edge WebDriver 来在 Edge 浏览器上运行我们的Selenium自动化测试脚本。

要求:

为了在Python中使用 Edge 和Selenium执行浏览器自动化,我们需要执行以下步骤:

  • Edge 浏览器: Microsoft Edge 浏览器可以从 https://www.microsoft.com/en-us/edge 下载。如果已经安装,我们可以使用 edge://version/ 获取现有版本的 edge
  • Selenium框架: Selenium是一个强大的工具,用于通过程序控制 Web 浏览器。它适用于所有浏览器,适用于所有主要操作系统。可以使用以下命令安装它:
pip install selenium
  • Edge WebDriver:可以从此 URL 下载Selenium Edge Webdriver。使用此驱动程序将在 Edge 浏览器中完成自动测试。根据操作系统下载edge webdriver,解压得到msedgedriver.exe。
  • 适用于 Microsoft Edge 的Selenium工具:此模块具有自动浏览器测试所需的功能,可以使用以下命令进行安装:
pip install msedge-selenium-tools selenium==3.141

下面是一个执行简单的自动化浏览器测试脚本的程序:

Python3
# import required modules
from selenium import webdriver
  
  
# Driver Code
if __name__ == '__main__':
  
    # create object
    edgeBrowser = webdriver.Edge(r"msedgedriver.exe")
  
    # open browser and navigate to facebook
    edgeBrowser.get('https://www.facebook.com')


Python3
# Import required module
from selenium.webdriver.opera.options import Options
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium import webdriver
from time import sleep
  
  
# Driver Code
if __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()


输出:

边缘打开 facebook 页面

在执行脚本时,我们可以看到,Edge 浏览器已打开 Facebook 页面,如图所示。您可以打开任何有效的网页,它会自动在边缘浏览器中打开提到的网页。

循序渐进的方法:

  • Edge 浏览器已打开。
  • 它被最大化。
  • 它打开一个网页(这里是 www.lambdatest.com)。
  • 它将用户电子邮件作为gfg@lambdatest.com 。
  • 获取提交按钮以单击表单中的开始免费测试按钮,它将导航到下一页,其 URL 为https://accounts.lambdatest.com/register?email=gfg@lambdatest.com 。

如果我们想知道一个网页的每个组件的id ,我们可以通过查看页面源代码检查相应的部分来获取它。

执行:

蟒蛇3

# Import required module
from selenium.webdriver.opera.options import Options
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium import webdriver
from time import sleep
  
  
# Driver Code
if __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()

输出:

自动化浏览器测试非常方便,因为每个浏览器都有单独的驱动程序,我们可以轻松地进行测试,无需手动操作。即使是自动化测试也更快,可以非常快速地测试多个测试页面并提供成功的测试结果。