Write Selenium Java binding code using Silk WebDriver

Introduction

In the previous article Try Silk WebDriver instead of Selenium IDE, I tried recording and playing Selenium scripts using Silk WebDriver. It was. This time, I will show you how to export the recorded script to Java source code and execute it as a JUnit test from Eclipse. This article is a continuation of the previous article, so

--Silk WebDriver is installed --The script is recorded on WebDriver

Please refer to the Previous article to proceed to this state.

Preparation

This time we will run JUnit tests on Eclipse, so first prepare the environment. Make sure you have the JDK installed on your machine. As of December 2017, Gradle shipped with Eclipse Oxygen installs JDK 8 because it seems that there is a problem with JDK 9 and it is not possible to create a project. If it is not installed, download it from here and install it. Next, click [PACKAGES] from the Eclipse site, download the [Eclipse IDE for Java Developers] package, and extract it to any location. Double-click the extracted eclipse.exe to start Eclipse.

Note


If your Gradle build fails with an error similar to the following:[Window]>[Preferences]Select[Preferences]Open a dialog
[Java]>[Installed JREs]Please set the JDK with.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Could not find tools.jar. Please check that C:\Program Files (x86)\Silk\SilkTest\ng\jre contains a valid
 JDK installation.

Create a Java project in Eclipse

When you're ready, create a project to add the Java files you want to export with Silk WebDriver. Select File> New> Project ... in Eclipse to open the New Project wizard.

image.png

This article uses a Gradle project, so select Gradle> Gradle Project and click the Next button.

image.png

After specifying an appropriate project name on the [New Gradle Project] screen, click the [Finish] button to complete the project creation.

Note


The source code output by Silk WebDriver is encoded in UTF-It is 8. If the script contains Japanese
Project encoding is also UTF-Change it to 8. Also, for Gradle, gradle.Create properties and
It's a good idea to add the following line:

org.gradle.jvmargs=-Dfile.encoding=UTF-8

image.png

You can delete the sample files included in the created project.

image.png

Then open the build.gradle file from Package Explorer and add the following line:

    compile 'org.seleniumhq.selenium:selenium-java:3.+'

image.png

Once added, save the file and refresh the project by choosing Gradle> Refresh Gradle Project from the context menu. The libraries required for Selenium Java binding have been added to the [Project and External Dependencies] of Package Explorer as follows. Now that you're ready, let's go back to Silk WebDriver and export the Java code.

image.png

Export Java code from Silk WebDriver

To export scripts with Silk WebDriver, you need to be running in login mode. This means that you need to create an account in the Micro Focus Build Portal. When you click the Don't have an account? Sign up link in the first Welcome dialog or in the dialog that appears when you try to execute a command that requires you to operate in login mode (such as Export or Save), You can register as a user. If you're following the steps in the article, the Silk WebDriver sidebar will open and you'll probably be recording. In the browser combo box at the top of the sidebarImage.png , In the program language combo box, selectImage.png and its Click the hamburger icon on the right (Image.png) Select Export to open the above dialog and click the Don't have an account? Sign up link.

image.png

The user registration page will be displayed. Enter the required information and click the [Register] button (it's free, so don't worry).

image.png

After registering, return to Silk WebDriver, click the Log in button in the previous dialog to display the login dialog, and log in by specifying the email address and password you specified during registration. When you are logged in, the Export Test Case dialog is displayed. Select the source folder of your project, enter an appropriate file name, and click the Save button.

image.png

When you return to Eclipse, open the context menu for your project and select Refresh. The file you just added will be displayed on Package Explorer, so open it and check it.

image.png

Run Selenium script in Eclipse

The exported and saved code has three methods.

setup
A method that is executed before the test is executed in the
class ( @Before annotation). Initialize the driver. Browser Since you selected Chrome in the combo box, the code that uses ChromeDriver has been generated.
myTestCaseName
Test code to execute ( @Test annotation). The Java code for the recorded Selenium script has been generated.
tearDown
Method executed after executing the test in the class ( @After annotation). Performs driver termination processing.

At this point, the rest is the same as running a normal JUnit test. If you want to run the myTestCaseName test, move the cursor to myTestCaseName in the editor, open the context menu, and select Run As> JUnit Test or Gradle Test to run the test.

Modify Selenium script

Finally, let's change the script. First, let's save the script with Silk WebDriver. Select Save or Save As from the hamburger icon to open the Save Recorded Actions dialog. Save the SWD format file by specifying the location and file name where you want to save the file. Now, when you exit Silk WebDriver, you can edit the script again on Silk WebDriver by loading it from the Open existing script or Files list on the Start screen, or by using the Open menu command in the sidebar. It will be like.

image.png

Put Silk WebDriver in record mode to try out the script changes. If the sidebar is closed, open the saved SWD file and view the script in the sidebar again. If you see the sidebar but Chrome isn't open, select Get URL'http: // ...' at the beginning of the recorded operation and click the play button. Also, if you are not in record mode (the icon is not displayed in the lower right corner of your browser), click the record button. When you record additional operations with Silk WebDriver, they are added to the end of the recorded operations. For example, if you check [Great Deals], "Click'plan_b'" will be added as follows.

image.png

This operation is the operation you want to perform after "Click'plan_a'", so drag and drop it. Once moved, let's run the modified script. Select the Recorded Actions node and click the play button. As expected, the "profitable sightseeing plan" was checked and the confirmation page for the reservation details was displayed. However, if you select "Advantageous sightseeing plan", the fee will be added by 1000 yen per person, so the verification will fail.

image.png

At this time, you cannot edit the properties of the operation you recorded on Silk WebDriver, so you will need to re-record the validation. Move the cursor to 23500 in your browser and press Ctrl + Alt to re-record the validation. The old verification is the delete button (![Image.png](https://qiita-image-store.s3.amazonaws.com/0/197245/b81d57ed-31ee-1bd2] that appears on the right when you move the cursor to the operation. -bca5-6fcafea977d5.png)) Click to delete. Select Recorded Actions again and click the play button, and this time everything should work fine. When you're done making your changes, choose Save from the menu to save your changes. To reflect this change in your Java code, press Ctrl + C in the sidebar or select Copy from the menu. When the operation is copied to the clipboard, you will see a message similar to the following at the bottom of the sidebar:

image.png

When you see this message, go back to Eclipse, delete all the statements in `` `myTestCaseName```, and press Ctrl + V to replace them with new statements. After saving the file, select Run As> JUnit Test or Gradle Test again to run the test.

in conclusion

The above is the basic procedure for generating and editing Selenium Java binding code using Silk WebDriver. In addition to Java / JUnit, Silk WebDriver can export source code for Ruby, Python, JavaScript, C #, VB and various frameworks. Select the program language and export according to your environment. The code used this time can also be accessed from GitHub, so please use it.

Recommended Posts