Selenium Visual Studio Code



Configuring selenium environment with Python + Visual Studio code

  1. Selenium Visual Studio Code
  2. Selenium In Visual Studio
  3. Selenium Visual Studio Code Tutorial

NuGet package restoring ready, and no need to commit 'chromedriver(.exe)' binary into source code control repository. For example, at the package manager console on Visual Studio, enter the following command. If you are using Chrome version 90: PM Install-Package Selenium.WebDriver.ChromeDriver -Version 90.0.4430.2400.

Using pip to install selenium

  • This video will explain how to setup and execute #selenium project in Visual studio code along with #testng. #vscode is a light weight ide from microsoft.
  • Selenium and Visual Studio Code are both open source tools. Visual Studio Code with 78.4K GitHub stars and 10.9K forks on GitHub appears to be more popular than Selenium with 14.7K GitHub stars and 4.92K.

Input directly from visual studio code console:

  • Check if selenium is installed successfully

Input directly in Visual Studio Code:

  • Install chrome driver and Firefox geckodriver

https://sites.google.com/a/ch…
https://github.com/mozilla/ge…

take chromedriver.exe 、 geckodriver.exe Put it in the python installation directory, or chromedriver.exe Add the directory to the environment variable path

Selenium Visual Studio Code

  • Check if selenium + Python + Visual Studio code is configured successfully

F5 run code, openhttps://cn.bing.com/, configured successfully

edition

  • Python version 3.6.4

  • Selenium version 3.141.0

  • Chrome version 70.0.3538.77 (official version) (64 bit)
  • Chrome driver version 2.43

Create a Coded UI Test using Selenium in Visual Studio

If you performed the previous lab, you will have used a screen recorder to create a coded UI test. While screen recorders are convenient, they often do not provide the level of control needed for more sophisticated testing.

In this lab, you will learn to create a coded UI test based on Selenium. The Selenium automation tools speak a language known as Selenese. Rather than issuing Selenese directly to the different types of browsers, web drivers are used to perform the translation. In this lab, you will install the Chrome, IE, and Firefox drivers, then automate the code from within the C# language in Visual Studio.

Note that there is a Selenium IDE that can be used for recording web actions but using it is not currently recommended. It only works with older insecure versions of FireFox (up to version 5.4). In this lab, we will use the drivers and classes directly from code.

For the purposes of the lab, we will use the PartsUnlimited web application hosted on Azure to run this lab.

There have been reports that the keyword search on the home page is intermittently performing poorly. We will investigate how the keyword search on the home page performs, both by entering values into the box and clicking the search icon, and by directly referencing the search term a URL.

DevOps MPP Course Source

  • This lab is used in course DevOps200.5x: DevOps Testing - Module 5.

Lab Video:

Pre-requisites:

  • Visual Studio 2017
  • Google Chrome (recent edition) https://www.google.com.au/chrome/browser/features.html
  • Access to the PartsUnlimited website deployed at http://cdrm-pu-demo-dev.azurewebsites.net/

Lab Tasks:

  • Create a coded UI web test in C#

Estimated Lab Time:

  • approx. 30 minutes

Learn to use the Parts Unlimited Search Functionality

  1. In a Chrome browser, launch the PartsUnlimited web application by going to the URL http://cdrm-pu-demo-dev.azurewebsites.net/
  2. This opens the PartsUnlimited application as shown below.

    Ensure that the website opens succesfully.

  3. On the home page, in the Search box, enter light and click the Search icon.

  4. Note that three products are returned as expected.

  5. Also note in the browser search bar that the search item has been added to the URL. We also want to test that direct access to the search works.

  6. Click the Parts Unlimited heading to return to the home page.

Locate the IDs of the required elements

  1. While in the home page, click F12 to open the developer tools.

  2. In the top section of the developer tools, click on the tool to select screen elements.

  3. Hover over the search box and note its ID.

    Note that the name is “search-box”.

  4. Hover over the search icon and note its ID.

    Note that the name is “search-link”.

    These are the elements that we will automate.

Create the test in Visual Studio

  1. Launch Visual Studio Enterprise.

  2. From the File menu, click New then Project. In the New Project window, choose a Unit Test Project from the Test category.

  3. Add a name for the project, and solution, and a location for the project.

  4. In Solution Explorer, right-click References and click Manage NuGet Packages.

  5. In the NuGet manager, click Browse, enter selenium as the search term and hit Enter to start the search.

  6. Install the following packages, then close the NuGet manager window:
    • Selenium.WebDriver
    • Selenium.WebDriver.ChromeDriver
    • Selenium.WebDriver.IEDriver
    • Selenium.Firefox.WebDriver
    • Selenium.WebDriver.PhantomJS.Xplatform
  7. In Solution Explorer, right-click UnitTest1.cs and rename it to PU_SearchTests.cs.

  8. When prompted to rename all references, click Yes.

  9. Add the following using statements after the ones that are already there:

  10. Your code should look like this:

  11. Add the following declarations to the beginning of the class. Note that the websiteURL and the required browser should really be parameters but this will be ok for our test:

  12. Your code should look like this:

  13. Add the following initialize and cleanup methods:

  14. Your code should look this this:

  15. Replace the test method with the following code:

  16. Your code should look this this:

Code

At this point, we have a test that should just start the browser (a Chrome browser), maximize the window, set the timeout, and go to the Parts Unlimited website. We can test it up to this point.

  1. From the Build menu, click Build Solution and make sure the solution builds correctly.

  2. In Test Explorer (if not visible, from the View menu, click to open it), right-click the SearchForLights test, and click Run Selected Tests to execute the test.

  3. You should see the browser open, maximize (if not already), then go to the Parts Unlimited home page.

  4. Add the following statements to the test method:

  5. Your code should look like this:

  6. This will now clear the search box, type the value “light” and search for it.

  7. From the Build menu, click Build Solution and make sure the solution builds correctly.

  8. In Test Explorer, right-click the SearchForLights test, and click Run Selected Tests to execute the test.

Selenium visual studio code python

You should see the browser open, maximize (if not already), then go to the Parts Unlimited home page, then search for the lights. You will briefly see the search results page appear before the browser closes.

Selenium In Visual Studio

We also needed to check a direct URL search.

Selenium Visual Studio Code Tutorial

  1. Add another test method below the current one:

  2. Your code should look like this:

  3. From the Build menu, click Build Solution and make sure the solution still builds correctly.

  4. In Test Explorer, right-click the DirectSearchForLights test, and click Run Selected Tests to execute the test.

Your new test should also run as expected.

Congratulations! You’ve now completed this lab.

For more information , you can see: Visual Studio: https://aka.ms/edx-devops200.5x-vs01 Web Apps: https://aka.ms/edx-devops200.5x-az03