Selenium on Linux Collector to execute synthetic webchecks

Recommended Posts

  • LogicMonitor Staff

You can use this bash script to install and configure all the necessary components in order to execute synthetic webchecks on a Linux (Debian/Ubuntu) Collector.  This script will install Chrome, Chromedriver, and Selenium to the Collector's host (be sure the Collector is installed prior to executing the bash script).

Once this is done, you can execute selenium commands from within LogicMonitor DataSources (sample DataSources can be found with in the LM-Exchange: "Synthetics Check Template" and "LogicMonitor Login Selenium"). 

The easiest approach I've found to getting a synthetics recording into LM is to use the Selenium IDE Chrome extension to record and then export the recording as a java file. From here, simply copy-paste the driver. commands from this exported file into the DataSource.  For simplicity and clarity, I've always applied the synthetics DataSource to the Collector itself.  Inherent in this approach is that you can have multiple synthetics DataSources applied to a single Collector.

The final result can look something like this: 


Though it is not depicted here in this example, you can insert timestamps into the DataSource groovy script to measure the duration of each step in your webcheck.

For more info on uploading other custom jars to the Collector, check out this Support Page

Edited by Jake Cohen
  • Like 3
  • Upvote 1
Link to post
Share on other sites

Thanks Jake. Your post was quite timely with my intentions to try Selenium with LogicMonitor and very much helped get me started (although I used CentOS collector machine, but still the bash-script gave the right direction of course! 🙂 

Those DataSources seemed to be private, so I could not load them, so I created one myself (included below; possibly very bad version, but it works 🙂 ).

In order to check textFields in the target web site, I also added two more libraries in addition to the selenium-server-standalone.jar:

And here is my example Groovy-script just to help others (maybe):

import com.santaba.agent.groovyapi.expect.Expect;
import com.santaba.agent.groovyapi.snmp.Snmp;
import com.santaba.agent.groovyapi.http.*;
import com.santaba.agent.groovyapi.jmx.*;
import org.xbill.DNS.*;
import static org.junit.Assert.*;
import static;
import static org.hamcrest.core.IsNot.not;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Keys;
import java.util.*;
ChromeOptions chromeOptions = new ChromeOptions();
    WebDriver driver = new ChromeDriver(options=chromeOptions);
//    driver.manage().window().setSize(new Dimension(1321, 916));
    driver.findElement(By.cssSelector("a:nth-child(4) > img")).click();
    assertThat(driver.findElement(By.cssSelector("h2:nth-child(56)")).getText(), is("Saksan reissu -Germany trip 2005"));

Link to post
Share on other sites

Thanks for the datasources; they confirmed what I was doing!

And then, just to finalize this, as my previous script was indeed a very crude and hastily comprised. I had to abandon all Asserts (including softAsserts), so a better generalized version below having two datapoints: FailedStep and GeneralSuccess (not really needed, but just for clarity). Two graphs as well having the same ones where FailedStep has vertical label description as "ZeroIsGood" and GeneralSuccess has "OneIsGood". Of course alert thresholds set respectively. This quite probably fits my needs nicely when cloning and amending to multiple different tests. (One development step would still be to get rid of the "--no-sandbox" as it is a security risk to run chromedriver as root.)

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.*;
ChromeOptions chromeOptions = new ChromeOptions();
WebDriver driver = new ChromeDriver(options=chromeOptions);
/////// STEP 1
try {
    if ( driver.getTitle().contains("404") )   //Only way to catch page not found (
        { println "GeneralSuccess=0"println "FailedStep=1"; driver.quit(); return 0; }
    } catch (Exception ex) { println "GeneralSuccess=0"println "FailedStep=1"; driver.quit(); return 0; }
/////// STEP 2
try {
    driver.findElement(By.cssSelector("a:nth-child(4) > img")).click(); //Click the motorbike image in the right frame
    if ( driver.getTitle().contains("404") )   //Only way to catch page not found (
        { println "GeneralSuccess=0"println "FailedStep=2"; driver.quit(); return 0; }
    } catch (Exception ex) { println "GeneralSuccess=0"println "FailedStep=2"; driver.quit(); return 0; }
/////// STEP 3
try {
    if ( !driver.findElement(By.cssSelector("h2:nth-child(56)")).getText().contains("Saksan reissu -Germany trip 2005") )
        { println "GeneralSuccess=0"println "FailedStep=3"; driver.quit(); return 0; }
    } catch (Exception ex) { println "GeneralSuccess=0"println "FailedStep=3"; driver.quit(); return 0; }
println "GeneralSuccess=1"println "FailedStep=0"; driver.quit(); return 0;
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.