Question: I have a scenario in which I need to execute the selenium scripts through servlets. I mean I will get the same input from the front end (Web UI) by passing those values to selenium classes then I will execute those classes.

I have tried this:

1. Created a servlet
2. Created an object for selenium class and called the main function in the servlet post method

But unfortunately, I am not getting any result or exceptions.

Answer: 
I have also added another solution which you wanted to implement ie) passing the values from the servlet post method to your selenium test

Download selenium java client driver [here](http://selenium-release.storage.googleapis.com/2.46/selenium-java-2.46.0.zip)

Extract the downloaded jar and add the selenium-java-2.46.0.jar and all the other jars in libs folder into /WEB-INF/lib folder of the deployed web app

I will add a basic script which i tested and was working fine as an example

**Servlet post method**

```
protected void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
    {

        resp.setContentType("text/html");

        String url=req.getParameter("url");

       //add the parameters to be passed to the main method of the selenium test into a string array

        String[] urls=new String[1];

        urls[0]=url;

        //pass the string array as an argument to the main method

        Seleniumclass.main(urls);

    }
}

```

**SeleniumTest**

```
    public class Seleniumclass {

        public static void main(String[] args) {

            WebDriver driver = new FirefoxDriver();

            driver.get(args[0]);
        }

}

```

**Note** : You cannot call the testng test directly from the servlet post method like main method bcause testng is a testing framework but you can write the post values from a servlet to a file and then trigger the testng.xml file from your servlet program.In your selenium script read the values from file

**Trigger testng xml file**

```
TestListenerAdapter adapter = new TestListenerAdapter();

TestNG testng = new TestNG();

List<String> suites = Lists.newArrayList();

//path to xml

suites.add("/home/vicky/testng.xml"); 

testng.setTestSuites(suites);

testng.run();

```

Hope this helps you...if you still face any issues kindly get back
The best approach is save the values from the servlet to a Database.

1. Install `continuous Integration server` like [**Jenkins**](https://jenkins-ci.org/) and create a Java project to check for new entries in database. Set cron job for this build. Actually Jenkins has all these features you just have to specify the time interval.

> 
> Example: If the time interval is set to 10 mins the script will run for every 10 mins . If new entries are present pass the build else fail the build.
> 
> 
> ![enter image description here](https://i.stack.imgur.com/tePCe.jpg)
> 
> 
> 

2. If the build is passed then execute the selenium script. There is an option for it in Jenkins "Build after other projects are built". In the Selenium script use the values from the database.

> 
> ![enter image description here](https://i.stack.imgur.com/mwIJy.jpg)
> 
> 
> 

3. In the database have a column like checkbox. Initially leave it unchecked update it after the selenium script has run.By this technique you can distinguish between new entries and old entries.

---

**Advantages of this approach (jenkins)**

* Build history of failures and successes of the project.
* E-mail notifications can be set up. Then test reports will be sent to the specific e-mails after execution. Any build failure can be reported as well.
* For more advantages of Jenkins look [here](https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Best+Practices).

Recently I've implemented a integrating servlet and Jenkins using the above technique.

Hope this helps you... if you have any queries kindly get back.