Selenium Web Driver - part 1-2024.pptx
Document Details
Uploaded by ThriftyHyperbole
Tags
Full Transcript
INFO-6105 In this lesson Overview of Selenium Web Driver Installing Selenium Web Driver Installing the required tools Downloading and unpacking Selenium Web Driver Setting up a Eclipse project for Selenium Web Driver Simple example of Web Driver use Selenium Web Driver Overview...
INFO-6105 In this lesson Overview of Selenium Web Driver Installing Selenium Web Driver Installing the required tools Downloading and unpacking Selenium Web Driver Setting up a Eclipse project for Selenium Web Driver Simple example of Web Driver use Selenium Web Driver Overview Selenium Web Driver is a replacement for the older Selenium Remote Control automation engine A very significant difference RC, like the IDE, uses JavaScript to execute the test cases/suites; this places limitations on what can be done because of the JavaScript security restrictions Web Driver does not use JavaScript, instead it uses languages like Java, C# and Ruby Selenium Web Driver Overview Since Web Driver does not have the JavaScript restrictions, it can test more web activities such as: File upload File download Other system access operations Selenium Web Driver Overview Web Driver is available for use with Java, Ruby, C#, Python, etc. Development work can be done using an IDE for the chosen language or using ordinary text editors e.g. Eclipse, NetBeans, vim, notpad++ Build tools can be used, e.g. maven (Java) make (any language) Selenium Web Driver Overview Maven, when properly configured, is designed to: Manage library dependencies Compile/build applications Run unit tests on the applications being built (needs Junit or similar) Run the application after it is built Maven dependency management works best when the dependency files are available from the maven web site Installing Web Driver Web Driver consists of programming language specific libraries, making it relatively platform independent All Web Driver libraries include support for the Firefox browser, but it may be necessary to install a driver utility (discussed later) Support for many other types of browsers must be downloaded and added to the development environment separately Components of Selenium WebDriver https://www.selenium.dev/documentation /en/webdriver/understanding_the_compon ents/ Installing Web Driver In addition to the Web Driver libraries, the following programs and utilities may be needed: Software development IDE, some offer development language specific versions (eclipse, NetBeans, etc.) Programming language tools (JDK, Python interpreter, etc.) Testing libraries (Junit, TestNg etc.) Other application specific libraries Build/run utilities (Maven, Jenkins, etc.) Runtime drivers (geckodriver, etc.) Installing Web Driver Download and install any build/run utilities you are going to use, do this first since some IDEs detect them during their installation process and are automatically configured to use them Make sure you get the right version of the tool for the IDE, programming language and operating system being used Installing Web Driver Download and install any programming language tools needed Make sure the tools get added to the operating system’s path. Test the basic function of the tools For java: java –version and javac – version Installing Web Driver Download and install the chosen software IDE If the chosen tool has a specific version for the programming language to be used, get that version. This will probably make creating and working with development projects easier Again, make sure that the operating system’s path is set properly Installing Web Driver Go to the Selenium web site and download the version of Web Driver and any browser support modules that you need for your work These may be tarballs, zip files or jar files, Extract them as necessary and put them in a directory (does not need to be in the path, just accessible by the development and build tools, and the runtime environments) Setting up a Web Driver test The following slides show how to set up and work with a Web Driver test as a simple Java project using the Eclipse IDE This example was created on a Windows system using JRE and JDK 21 https://www.oracle.com/ca-en/java/technologies/downloads/#jdk21-windows Download the latest stable version of Eclipse. This example uses Eclipse Version: 2023-12 (4.30.0) https://www.eclipse.org/downloads/packages/ Web Driver 4.21.0 (May 16, 2024) https://www.selenium.dev/downloads/ Selenium standalone server Chromedriver Junit 5 Setting up a Web Driver test Right click in the left navigation pane of eclipse Select ‘New’ on the context menu Select ‘Java Project’ from the pop-up menu Setting up a Web Driver test On the first page of the ‘Create a new project’ wizard, enter a name for your project All other options can remain at their default values Click ‘Finish’ Setting up a Web Driver test You should be able to view your project from Create a folder called jars the left windows pane. Copy and paste the jar files from the selenium-java-4.21.0 folders to the jar folder. Setting up a Web Driver test Now we need to add the jar files in this project’s build path. Click on the root folder of the project Select Build Path -> Configure Build Path. Click on libraries (since we have copied the jars in the project locally, I am going to click on add JARS). If you have not copied the files in your project, you can add external jars options instead and navigate to the directories where the jars are located. Setting up a Web Driver test Select the list of jar files. Click ‘OK’ Setting up a Web Driver test The jars are shown in the Referenced Libraries. The project is set for going to the next step. Setting up a Web Driver test After setting up the project, it is time to start adding Java classes to it Create a package after right-clicking the src folder. Right-click in the navigation area, select ‘New’ from the connect menu, and then ‘Class’ from the pop-up menu Enter a name for the class and click ‘Finish’, leaving all the other settings on their default values. Setting up a Web Driver test The class is now listed in the navigation pane and its skeleton is added to an edit tab ready for details to be typed. Setting up a Web Driver test For the first test, simply load a page. This is the simple wall posting form as was used for the IDE example last week. The simple test code This example shows how both ChromeDriver and FirefoxDriver can be instantiated Line 3 creates an instance of the ChromeDriver which is then used on line 7 to launch a browser and load the page into it. Line 5 creates an instance of the FirefoxDriver which is then used on line 8 to launch a browser and load the page into it. Running the simple test To run this simple test or any other test, select the file you want to run from the drop-down option of the run button, and click to execute that test. The results of the build and run operations are displayed at the bottom of the eclipse window. Running the test Launching this test opens an actual instance of the Firefox browser and loads the page into it. Note the messages at the bottom of the screenshot, these are in the eclipse message area Even though they are red they are not errors, they are reporting the state of the driver needed to launch the browser Extending the Web Driver test The test created in the previous slides only loads the page, it does not do anything The next slides show how to extend it to fill in the form fields and submit the form Note that in a real test script, error handling is needed to catch and report exceptions properly, the findElement() method throws them when it cannot find the specified element Extending the Web Driver test The test case now fills in the form and submits it It does not yet do automatic checking of the results Checking test results Selenium Wed Driver is really good at running the test cases, but it does not include the functionality to evaluate the results of a test Test result evaluation must be done using other tools such as JUnit and TestNG The following slides use JUnit Adding Junit to your test You can add JUnit to your project at the same time as you added the Web Driver jar files If Junit is not already added, right click on the project name in the left pane of eclipse and select ‘Properties’, on the properties page select ‘Java Build Path’ in the left column and then select the ‘Libraries’ tab Proceed to add the JUnit 5 in your classpath. Add Junit library in the classpath Integration of Junit with Selenium Checking the test results When the test runs successfully, the assertEquals() method terminates normally When an error occurs, an exception is thrown: Exception in thread "main" org.opentest4j.AssertionFailedError: expected: but was: at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151) at org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132) at org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197) at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:182) at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:177) at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1145) at simple_form.SimpleFormTest.main(SimpleFormTest.java:33) Creating a separate JUnit test case Next lesson Web Driver functionality for: Selecting elements on a page Mouse actions Keyboard Actions Taking screen shots References https://www.selenium.dev/downloads/ https://junit.org/junit5/docs/current/user-guide/index.html