Executing JavaScript code

Parent Previous Next

Executing JavaScript code


A special ExecuteJavaScriptAsync method has been added to HatFramework to execute JavaScript code.


First, you need to declare a text variable and describe in it the script that you want to execute.


string script = @"(function(){ 

var element = document.getElementsByTagName('h2')[0]; 

return element.outerText; 

}());";


Now, using the ExecuteJavaScriptAsync method, we will execute the above script and get the result.


string actual = await tester.ExecuteJavaScriptAsync(script);


In this example, you will get the text from the H2 element from the page where this script was called.



Full example:


File: ExampleTest4.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Windows.Forms;

using System.Threading;

using System.Threading.Tasks;

using System.IO;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Text.RegularExpressions;

using System.Net;

using System.Net.Http;

using System.Net.Http.Headers;

using System.Reflection;

using Newtonsoft.Json;

using HatFramework;


namespace Hat

{

    public class ExampleTest4

    {

        Tester tester; // The main variable for the autotest operation


        // The main input function (the autotest starts with this function)

        public async void Main(Form browserWindow)

        {

            tester = new Tester(browserWindow); // Initializing the main variable


            await setUp(); // The function of getting started with the autotest

            await test(); // The function of performing test actions

            await tearDown(); // Autotest shutdown function

        }


        public async Task setUp()

        {

            await tester.BrowserFullScreenAsync(); // Sets the browser resolution to full screen

        }


        public async Task test()

        {

            await tester.TestBeginAsync(); // The beginning of the execution of actions

            await tester.GoToUrlAsync("https://somovstudio.github.io/test_eng.html", 5); // Loading the page at the specified address

            

            // Declaring a variable with a JavaScript script description

            string script = @"(function(){ 

                   var element = document.getElementsByTagName('h2')[0]; 

                   return element.outerText; 

                   }());";


            // Script Execution

            string actual = await tester.ExecuteJavaScriptAsync(script);


            // Checking the result

            string expected = "Authorization";

            await tester.AssertEqualsAsync(expected, actual); // The expected and actual data must match


            await tester.TestEndAsync(); // Completing actions

        }


        public async Task tearDown()

        {

            await tester.BrowserCloseAsync(); // Closes the browser

        }

    }

}


Examples of JavaScript and jQuery scripts for the method ExecuteJavaScriptAsync :


- focus on the input element and data entry


var element = document.evaluate("//input[@id='phone']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

element.focus();

element.value = '9999999999';

element.setAttribute("value", "9999999999");


- focus on the input element and insert data


document.getElementById("phone").focus();

document.execCommand("insertHTML", false, "9999999999");


- entering data into input and calling events


var element = document.evaluate("//input[@id='phone']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

element.value = '9999999999';

element.dispatchEvent(new KeyboardEvent('keydown', { bubbles: true }));

element.dispatchEvent(new KeyboardEvent('keypress', { bubbles: true }));

element.dispatchEvent(new KeyboardEvent('keyup', { bubbles: true }));

element.dispatchEvent(new Event('input', { bubbles: true }));

element.dispatchEvent(new Event('change', { bubbles: true }));


- highlight the value in the input


var element = document.evaluate("//input[@id='phone']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

element.select();


- mouse click


var clickEvent = new MouseEvent("click", { "view": window, "bubbles": true, "cancelable": false });

var element = document.evaluate("//input[@id='phone']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

element.dispatchEvent(clickEvent);


var theEvent = document.createEvent("MouseEvent");

theEvent.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);

element.dispatchEvent(theEvent);


- using jQuery to click a button (if the library is connected on the page under test)


string script = @"$(function() { 

console.log('autotest JQUERY');  

$('#btn-send > div.btn > a').click(); 

});";




Created with the Personal Edition of HelpNDoc: Full-featured Kindle eBooks generator