question

Matt J. avatar image
Matt J. asked ·

How do we add timers to Selenium - Web Driver?

The online docs I found all seem to reference older material for the deprecated Browser Agent and the dynaTrace Selenium Runner / dynaTrace Selenium Helper. None of this appears to be relevant as we are using Selenium / Web Driver scripts and UEM has replaced the browser agent.

Essentially what we are trying to do is what the below article states around "timers" but with our particular config.

https://community.dynatrace.com/community/display/PUB/How+to+include+dynaTrace+in+your+Selenium+Tests

appmonextensionspluginsappmon 77.1continuous deliverytest automation
1 comment
10 |2000000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 50.0 MiB each and 250.0 MiB total.

Parallel to this - anyone know how to add the Dynatrace headers to Selenium / Web Driver scripts aka https://www.dynatrace.com/support/doc/appmon/shortlink/id_integration_with_web_load_testing_and_monitoring_tools#request-header

0 Likes 0 · ·
Heather M. avatar image
Heather M. answered ·

Hi Sonja, here is the console output from the Jenkins job...again, if I remove the second test, it connects to the REST interface on the first try.

...

End DYNATRACE SESSION::: e2301957-ee20-4251-9bba-128799886a3d
Dynatrace AppMon Plugin - build tear down...
Finishing test run with ID=e2301957-ee20-4251-9bba-128799886a3d
Sleeping for the configured delay of 60 seconds before retrieving test run data from Dynatrace Server...
Connecting to Dynatrace Server REST interface... (ID=e2301957-ee20-4251-9bba-128799886a3d)
Sleeping for the configured delay of 60 seconds before retrieving test run data from Dynatrace Server... re-try 1 out of 4
Connecting to Dynatrace Server REST interface... (ID=e2301957-ee20-4251-9bba-128799886a3d)
Sleeping for the configured delay of 60 seconds before retrieving test run data from Dynatrace Server... re-try 2 out of 4
Connecting to Dynatrace Server REST interface... (ID=e2301957-ee20-4251-9bba-128799886a3d)
Sleeping for the configured delay of 60 seconds before retrieving test run data from Dynatrace Server... re-try 3 out of 4
Connecting to Dynatrace Server REST interface... (ID=e2301957-ee20-4251-9bba-128799886a3d)
Sleeping for the configured delay of 60 seconds before retrieving test run data from Dynatrace Server... re-try 4 out of 4
Connecting to Dynatrace Server REST interface... (ID=e2301957-ee20-4251-9bba-128799886a3d)
Report summary: {Failed=0, Degraded=0, Volatile=0, Improved=0, Passed=1}.
Finished: SUCCESS

1 comment Share
10 |2000000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 50.0 MiB each and 250.0 MiB total.

It looks to me like the AppMon server needs more time to process the data - on the last retry, we see that the first test (Passed=1) has been analyzed but the second hasn't yet been processed.

I would try to augment the delay for fetching the data on Jenkins. If I remember correctly, two minutes should be enough.

0 Likes 0 · ·
Heather M. avatar image
Heather M. answered ·

Hi Sonja! Matt and I are working on this together. So per your last comment, I modified our selenium script to do the following:

1. open the browser

2. set the DT_TESTRUNID

3. For the first "transaction" of the test, defined as some functionality in the UI

(1) set the DT_TESTNAME using js.executeScript("sessionStorage.DT_TESTNAME = \"first Test\";");

(2) do the actual test stuff

(3) end the visit using js.executeScript("dynaTrace.endVisit();");

4. For the second "transaction" of the test, defined as some functionality in the UI

(1) set the DT_TESTNAME using js.executeScript("sessionStorage.DT_TESTNAME = \"Second Test\";");

(2) do the actual test stuff

(3) end the visit using js.executeScript("dynaTrace.endVisit();");

5. Close the browser.

When I only run with one transaction, say first Test, and remark out the second transaction, everything works OK, the REST calls at the end of the Jenkins job are successful, and the test automation dashlet is updated. When I call both tests, and set the testname and end the visit for each before closing the browser, the REST calls fail, and the test automation dashlet does not update.

Am I to draw the conclusion that I cannot have a test run broken down into component functionality pieces, as Matt described - results for Login, results for Add Item, results for LogOut - in a single test? Or are we just doing something wrong?

Thanks!!

Here is our Selenium script:

package dyna;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.RandomStringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;

public class Main {
	static WebDriver _Browser ;
	static JavascriptExecutor js;
	static String _DynaTestName;
	static String dynaTstId;
	
	public static void main(String[] args) throws Exception{
	/***********************************************************************
         * Get everything started for the first time
         **********************************************************************/
		String ChromDriverDirectory = ".\\Package\\BrowserDrivers\\Chrome Driver			\\chromedriver.exe";
		DesiredCapabilities chromeCapabilities = DesiredCapabilities.chrome();
		System.setProperty("webdriver.chrome.driver", ChromDriverDirectory);
		_Browser = new ChromeDriver(chromeCapabilities);
		_Browser.get("http://10.42.178.0:8180/");
		js = (JavascriptExecutor) _Browser;
		
		InitializeDynatrace();
		
		/**********************************************************************
	     	* Run the first test
	     	* 1. Set the DynaTestName
	     	* 2. Start the test (transaction)
	     	* 3. End the transaction
	     	**********************************************************************/
		SetDynatraceTestName("EditAmountTest");
		EditAmountTest();
		endDynatraceVisit();
		
		/***********************************************************************
	     	* Run the second test
	     	* 1. Set the DynaTestName
	     	* 2. Start the test (transaction)
	     	* 3. End the transaction
	     	***********************************************************************/
		SetDynatraceTestName("OriginalUITest");
		OriginalUITest();
		endDynatraceVisit();
		
		/***********************************************************************
	     	* Close the browser down, we're done
	     	***********************************************************************/
		_Browser.close();
	}

	/*******************************************************************************
     	* Edit Amount test
     	*******************************************************************************/
	public static void EditAmountTest() throws Exception {
		// TEST HERE
	}
	
	/*******************************************************************************
     	* Original test
     	*******************************************************************************/
	public static void OriginalUITest() throws Exception {
		// SECOND TEST HERE
	}
	
	public static void InitializeDynatrace(){
		dynaTstId =System.getenv("dtTestrunID");
		try {
		   if(dynaTstId !=null) {
  		   System.out.println("DynatraceID:::"+dynaTstId);
    		   js.executeScript("sessionStorage.DT_TESTRUNID = \""+dynaTstId+"\";");
		}
		}catch(Exception e) {
		   System.out.println("Dynatrace Exception "+e.getMessage());
		   }
		}
		
	public static void SetDynatraceTestName(String testname){
		js.executeScript("sessionStorage.DT_TESTNAME = \""+testname+"\";");    	
	}	
	
	public static void endDynatraceVisit(){
		try {
		if(dynaTstId !=null) {
		  js.executeScript("dynaTrace.endVisit();");
		  System.out.println("End DYNATRACE SESSION::: "+dynaTstId);
	    	}
		} catch (Exception e) {
		  System.out.println("ERROR DYNATRACE END SESSION::: "+e.getMessage());
		  }
		}
1 comment Share
10 |2000000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 50.0 MiB each and 250.0 MiB total.

Hi! what do you mean by the REST calls fail? which result do you get back? also did you wait a little bit longer? It takes some time for the visits to be fully processed.

Typically, the recommended integration scenario will be to have only 1 visit for the whole test - and to look at the user actions for specific test steps. But the approach you are trying should also work. The only limitation that comes to my mind is that the second visit will only be started after your applications is making server side calls.

0 Likes 0 · ·
Sonja C. avatar image
Sonja C. answered ·

one UEM visit = one test (that will show up individually in this dashlet). To achieve what you what, you need to programatically end the visit:

js.executeScript("dynaTrace.endVisit();");

And then re-set another test name:

js.executeScript("sessionStorage.DT_TESTNAME = \"myTest\";");

See also "Tag the UEM visit as a test" in the documentation: https://www.dynatrace.com/support/doc/appmon/conti...

Share
10 |2000000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 50.0 MiB each and 250.0 MiB total.

Matt J. avatar image
Matt J. answered ·

OK but is there a way to define transactions within a script and have it show up in the test results dashlet? For example my test is called SELENIUM UI TEST but I was hoping to have the transactions defined so we could have a "launch" a "login" and a "search" so we have a breakdown of metrics captured for each transaction. Rather than just see a sum of DB calls and bytes received for the test, we would like to see it based on a transaction. So The "Launch" transaction made X DB calls and had Y bytes received, and the "Login" transaction made Z DB calls and had W bytes received etc. It looks like it was possible through the old way.


Share
10 |2000000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 50.0 MiB each and 250.0 MiB total.

Sonja C. avatar image
Sonja C. answered ·

Here is the latest documentation on integrating AppMon with Selenium (using UEM - User Experience Management & the JavaScript Agent): https://www.dynatrace.com/support/doc/appmon/conti...

Timers concept of the deprecated Browser Agent has been replaced with the JavaScript Agent / UEM visit (= test) and actions (= test steps). You can find more details on configuring the UEM action in the UEM documentation:

Hope this helps!

Sonja

Share
10 |2000000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 50.0 MiB each and 250.0 MiB total.