- Created by SS Doc Editor, last modified on Dec 10, 2021
The following section describes how to create a test case with an action component that will execute an existing Selenium script.
- Create an empty test case with any name
Note Adding path of chromedriver.exe/geckodriver.exe and Webdriver.dll needs to be done once per test. Also, both paths are cached for future test use. |
The new Selenium component comes with code to initialize the WebDriver for every VU when the test starts, and dispose the WebDriver when the test ends. The sample code is below:
#region Initialize/Dispose IWebDriver driver; IJavaScriptExecutor js; /// <summary> /// Fired when test started. /// </summary> public override void OnTestStart() { InitChromeDriver(); } void InitChromeDriver() { ChromeDriverService service = ChromeDriverService.CreateDefaultService(System.IO.Path.Combine(this.RuntimeVU.TestDirectory, @"Scripts\selenium")); service.HideCommandPromptWindow = true; ChromeOptions options = new ChromeOptions(); options.AddArguments(new List<string>() { "disable-extensions", "disable-gpu", "no-sandbox", //"headless" //uncomment to hide browser window. }); driver = new ChromeDriver(service, options); js = (IJavaScriptExecutor) driver; } /// <summary> /// Fired when test ends. /// </summary> public override void OnTestEnd() { driver.Quit(); } #endregion
Creating selenium transactions
The existing Selenium script must be copied into one or more action methods. Each method will represent a transaction that will later be added to the test case tree and appear on the test run report. Below is an example:
/// <summary> /// Direct the driver to navigate to our website. /// </summary> /// <param name="arg"></param> public void NavigateToWebsite(ActionArgs arg) { driver.Navigate().GoToUrl("https://www.mywebapplication.com"); } /// <summary> /// Direct the driver to click on a link. /// </summary> /// <param name="arg"></param> public void ClickOnALink(ActionArgs arg) { driver.FindElement(By.LinkText("My Link")).Click(); }
Adding transactions by highlighting code
Another way to add action methods to an existing Selenium script is to do the following:
- Copy and paste the entire Selenium into the class definition inside the editor.
- Select a code snipet snippet to add to an action method
- Click the Create an action method from selected script button to bring up the surround with helper
- Select the action, click, or navigate option to create an appropriate action method with the selected code inside
- Give the action method a name
- Repeat steps 2-5 until all the code is inside appropriate action methods.
Creating transactions from the action methods
To automatically create transactions and containing action objects on the test case tree click the Compile the Selenium component and add actions to the current test case button.
- No labels