Forum Discussion

Jake_Cohen's avatar
Jake_Cohen
Former Employee
5 years ago

Selenium on Linux Collector to execute synthetic webchecks

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

  • 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:

    wrapper.java.classpath.115=../custom/selenium-server-standalone.jar
    wrapper.java.classpath.116=../custom/hamcrest-2.2.jar   
    wrapper.java.classpath.117=../custom/junit-4.13.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 org.hamcrest.CoreMatchers.is;
    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.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    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.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.Alert;
    import org.openqa.selenium.Keys;
    import java.util.*;
    import java.net.MalformedURLException;
    import java.net.URL;
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("--no-sandbox");
    //chromeOptions.addArguments("--remote-debugging-port=9222");
    //chromeOptions.addArguments("--disable-dev-shm-usage");
    //chromeOptions.addArguments("--disable-gpu");
    chromeOptions.addArguments("--headless");
        WebDriver driver = new ChromeDriver(options=chromeOptions);
        driver.get("http://juhani.exdecfinland.org/~tforsell/home.html");
    //    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"));
        driver.close();
        driver.quit();
    println("TeijoScriptStatus=Success");

  • Jake_Cohen's avatar
    Jake_Cohen
    Former Employee

    Glad to hear you found this helpful @Teijo Forsell! I have just confirmed that the LM-Exchange DataSources posted above should now be public.  Thanks for sharing your DataSource template as well!

  • 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.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.WebElement;
    import java.util.*;
    import java.net.MalformedURLException;
    import java.net.URL;
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("--no-sandbox");
    chromeOptions.addArguments("--headless");
    WebDriver driver = new ChromeDriver(options=chromeOptions);
    /////// STEP 1
    try {
        driver.get("http://juhani.exdecfinland.org/~tforsell/home.html");
        if ( driver.getTitle().contains("404") )   //Only way to catch page not found (https://stackoverflow.com/questions/17657037/catching-a-404-error-with-selenium)
            { 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 (https://stackoverflow.com/questions/17657037/catching-a-404-error-with-selenium)
            { 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;
  • Anonymous's avatar
    Anonymous

    LM has made some serious advances with selenium and synthetic web checks. I'd reach out to your CSM to get in touch with the product managers about it.