Selenium IDE User Guide 4 (Code Export)

Posted May 28, 20207 min read

\ [](# getting-started) Getting started

You can right-click a test or suite, select Export, select the target language, and click to export the test of the test or suite to the WebDriver code Export.

Code Export Right Click
Code Export Menu

This will save the file containing the exported target language code to the browser's download directory.

\ [](# origin-tracing-code-comments) Origin tracking code comments

When exporting, there is an optional toggle switch to enable source tracking code comments.

This places the inline code comments in the exported file, which contains detailed information about the test steps in the Selenium IDE that generated the file.

\ [](# supported-exports) Supported exports

Currently, export to the following languages and test frameworks is supported.

  • C # NUnit
  • Java JUnit
  • JavaScript Mocha
  • python pytest

We intend to support all officially supported Selenium programming language bindings(eg Java, JavaScript, C #, Python and Ruby) in at least one testing framework for each language.

Help is welcome to add new languages and testing frameworks for specific languages. For more information about [how to do](# how-to-contribute), see [how to contribute](# how-to-contribute).

\ [](# c-nunit) C # NUnit

The C # NUnit export code is built to be used with .NET Core , NUnit 3.11 and the latest version of Selenium.

To create a new boilerplate project for use with NUnit, use the following dotnet new command.

dotnet new nunit -n NUnit-Tests --framework netcoreapp2.0

Using the following .csproj file, you can use the following dotnet restore command to install the correct package and version.

<!-filename:example.csproj->
<Project Sdk = "Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework> netcoreapp2.0 </TargetFramework>

    <IsPackable> false </IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include = "nunit" Version = "3.11.0" />
    <PackageReference Include = "NUnit3TestAdapter" Version = "3.13.0" />
    <PackageReference Include = "Microsoft.NET.Test.Sdk" Version = "16.0.1" />
    <PackageReference Include = "Selenium.Support" Version = "4.0.0-alpha03" />
    <PackageReference Include = "Selenium.WebDriver" Version = "4.0.0-alpha03" />
  </ItemGroup>

</Project>


> dotnet restore example.csproj

\ [](# c-xunit) C # xUnit

The exported code of C # xUnit is built to be used with C #, xUnit and the latest version of Selenium.

Just like C # Nunit, you can install it using the dotnet tool and run it after installing these dependencies(for example, use Install-Package Selenium.WebDriveror or dotnet add package Selenium.WebDriver).

To create a new boilerplate project for use with xUnit, use the following dotnet new command.

> dotnet new xUnitTests

Using the following .csproj file, you can use the following dotnet restore command to install the correct package and version.

<!-filename:example.csproj->
<Project Sdk = "Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework> netcoreapp2.0 </TargetFramework>

    <IsPackable> false </IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include = "xunit" Version = "2.4.1" />
    <PackageReference Include = "Microsoft.NET.Test.Sdk" Version = "16.0.1" />
    <PackageReference Include = "Selenium.Support" Version = "4.0.0-alpha03" />
    <PackageReference Include = "Selenium.WebDriver" Version = "4.0.0-alpha03" />
  </ItemGroup>

</Project>


> dotnet restore example.csproj

\ [](# java-junit) Java JUnit

The exported code of Java JUnit can be used with Java 8, JUnit 4.12 and the latest version of Selenium.

You should be able to put the exported Java file into the standard Maven directory structure with the files listed in pom.xml and run it.

This is a sample pom.xml to help you get started.

<project>
  <modelVersion> 4.0.0 </modelVersion>
  <groupId> org.seleniumhq.selenium </groupId>
  <artifactId> selenium-ide-java-code-export </artifactId>
  <version> 1 </version>
  <url> http://maven.apache.org </url>
  <dependencies>
    <dependency>
      <groupId> junit </groupId>
      <artifactId> junit </artifactId>
      <version> 4.12 </version>
      <scope> test </scope>
    </dependency>
    <dependency>
      <groupId> org.seleniumhq.selenium </groupId>
      <artifactId> selenium-java </artifactId>
      <version> 4.0.0-alpha-3 </version>
    </dependency>
  </dependencies>
</project>

\ [](# javascript-mocha) JavaScript Mocha

The exported code of JavaScript Mocha is built to work with Node 10, Mocha 6.1.x and the latest version of Selenium.

After installing these dependencies(for example, using npm install), you should be able to get the exported JavaScript file and run it.

This is an example package.json that can help you get started.

{
  "dependencies":{
    "mocha":"^ 6.1.4",
    "selenium-webdriver":"^ 4.0.0-alpha.3"
  }
}

\ [](# python-pytest) python pytest

The exported code of Python pytest can be used with Python 3, pytest 4.6.x and the latest version of Selenium.

After installing these dependencies(for example, using pip3 install), you should be able to get the exported JavaScript file and run it.

This is an example requirements.txt to help you get started.

pytest == 4.6.3
selenium == 4.0.0a1


> pip3 install -r ./requirements.txt

\ [](# ruby-rspec) Ruby RSpec

The exported code of Ruby Rspec can be used with Ruby 2.6.x, RSpec 3.9.x and the latest version of Selenium.

By using Bundler and the following tools, Gemfile you can install necessary dependencies.

# Gemfile
source 'https://rubygems.org'

gem 'selenium-webdriver'
gem 'rspec'


> gem install bunder
> bundle install

\ [](# how-to-contribute) How to contribute

Code export is built in a modular manner to help achieve contributions.

Each language and testing framework has its own package containing the code to be exported. Each code segment maps to commands in Selenium IDE, and each package depends on the underlying "core" package, which takes care of all the heavy lifting.

The following are the steps to create code for a new language or a new testing framework in an established language.

\ [](# 1-create-a-new-package) 1. Create a new package

First, copy an existing language package(eg packages/code-export-java-junit), and then rename it(eg the details in the folder and file package.json) as you want Contributed target languages and frameworks(eg packages/code-export-ruby-rspec, etc.).

Next, add the new package as dependent on [the package.json in code-export]( https://github.com/SeleniumHQ/selenium-ide/blob/c55c556ffc947fd3f6ee8ab317915c6f879a88dc/packages/code-export/package.json # L22).

Finally, yarn runs from the root directory of the project, and then uses the build project yarn watch( here Full details of the local build).

\ [](# 2-update-the-locators-and-commands) 2. Update locators and commands

The basis for code export is language-specific strings, which are converted into output codes. The most prominent of these are commands and locator strategies(for example, the syntax of "by" lookup).

For a given language, there is a file for each language, and the accompanying test file.

You can see an example in packages/code-export-java-junit.

When declaring a new command, you can specify its output as [string]( https://github.com/SeleniumHQ/selenium-ide/blob/v3/packages/code-export-java-junit/src/command.js# L192), you can also specify is specify indentation Level object .

Built into the code export is a prefix control that controls the indentation of the output code. This structure is useful if the output of the command is verbose and you want to display it explicitly. Or, if the command changes the indentation level of the immediately following command.

\ [](# 3-create-the-hooks) 3. Create hooks

Hooks constitute most of the structure of the code to be exported(for example, suites, tests, and everything contained in them, such as settings, disassembly, etc.). They also enable plug-ins to export code to different parts of a test or suite.

There are 9 different hooks:

  • afterAll(after all tests are completed)
  • afterEach(after completing each test-before afterAll)
  • beforeAll(before running all tests)
  • beforeEach(before running each test-after beforeAll)
  • command(issue code for new commands added by the plugin)
  • dependency(add other language dependencies)
  • inEachBegin(at the beginning of each test)
  • inEachEnd(at the end of each test)
  • variable(declare new variables that will be used throughout the suite)

You can see an example of implementing hooks at packages/code-export-java-junit:[hook]( https://github.com/SeleniumHQ/selenium-ide/blob/v3/packages/code-export -java-junit/src/hook.js)

\ [](# 4-update-the-language-specific-attributes) 4. Update language-specific attributes

You need to specify some low-level details in each language. Things like how much space to indent, how to declare methods, tests, suites, etc.

You can see the implementation of this example here in packages/code-export-java-junit:[language specific options]( https://github.com/SeleniumHQ/selenium-ide/blob/v3/packages/code- export-java-junit/src/index.js)

\ [](# 5-add-it-to-the-mix) 5. Add it to the mixture

After you have everything else ready, you can connect it for use in the UI.

This is possible packages/code-export/src/index.js .

You need to add the language to availableLanguages.

\ [](# 6-test-and-tune) 6. Test and adjust

The best end-to-end test for exporting code is to export a series of tests and verify that they are running as expected.

From the development version, you can access the seed test. This is a good starting point to verify that all standard library commands are applicable to your new language.

Test, repair and test again until you are confident in the final result.

  1. Selenium IDE User Guide 1(Crawler Script Recorder)
  2. Selenium IDE User Guide 2(Command Line Runner)
  3. Selenium IDE User Guide 3(Control Flow)
  4. Selenium IDE User Guide 4(Code Export)
  5. Selenium IDE User Guide 5(FAQ)
  6. Selenium IDE User Guide 6(Instruction List)
  7. Selenium IDE User Guide 7(Parameter Definition)

Author: distributed programming
Source: https://zthinker.com/
If you like this article, please long press the QR code and follow Distributed Programming
. Distributed programming