Pages

Saturday, 16 December 2017

Run Maven Project Using Jenkins

   Hi Friends,

   In this blog we will see how we can configure a Maven project in Jenkins on Ubuntu. If you are using other OS the don't worry. process is same for all.

    Prerequisites :

  1. Make sure you have successfully installed Java and all path are set
Open terminal and run the commands :

     echo $JAVA_HOME

     Java -version 


   2. Make sure you have successfully installed Maven on your system and maven home path is set in system path.
       
   Open terminal and use below commands to check whether maven installed on the system and all path are set

  ●     echo $M2_HOME
  ●     Mvn -v



   3. Make sure you have successfully installed Jenkins with all required plugins for Maven project

Plugin for Maven project
  ●  Maven Integration plugin
  ●  Maven Invoker plugin
  ●  Config File Provider Plugin

After successful installation just cross verify weather all things are set up.  start Jenkins > open Jenkins homepage > click New Item.
You will see here Maven Project to create a Jenkins Job with maven project configuration as shown below


    4. Now setup the java and maven path in Jenkins

       Go to Manage Jenkins > Global Tool Configuration and set MAVEN_HOME
       and JAVA_HOME



    5. Configure your pom.xml in Jenkins Job

     I. Navigate your project directory structure and copy the complete path till pom.xml e.g. in my case its           

 /home/narendra/workspace/RContactsWeb/pom.xml

    II. Now create a new job > Select Maven Project and enter the pom.xml 
        path here :




       After successful configuration > build the project. for status you can check console output 

Wednesday, 29 November 2017

Install and configure Maven on Ubuntu

This blog covers one alternative to install and configure Maven in Ubuntu. If you are facing some issue during installation by command line then go with below steps-

Step 1 : Navigate to  https://maven.apache.org/download.cgi and download the available version of Apache build tool apache-maven-3.5.2-bin.tar.gz file for Ubuntu


Select the binary distribution to download.


Step 2 : After successful download, place the downloaded file in your desired location directory and extract the same

Step 3 :Add maven in environment path : Open the terminal and run the following commands to set the environment variables (make sure you are providing right directory path where your maven available )

$ export M2_HOME=/usr/local/apache-maven/apache-maven-3.3.9

$ export M2=$M2_HOME/bin

$ export MAVEN_OPTS=-Xms256m -Xmx512m

with M2_Home path corresponding with the location of your extracted Maven files.
Now append the M2 variable to the system path:
$ export PATH=$M2:$PATH

Finally, verify if Maven has been added by running:
$ mvn -version

If this command showing maven version then every thing fine. Enjoy :)

Friday, 11 August 2017

How to use JavascriptExecutor in Selenium ?

Hi Friends,

I'm explaining some scenarios regarding JavascriptExecutor. Which i have experienced during Selenium automation.

So first thing come in mind is what is JavascriptExecutor ?
Expertise whose daily routine to play with Selenium may be aware but if you are new to Selenium then you are on right place. JavascriptExecutor is an interface which allows you to execute JavaScript code in Selenium Webdriver and perform your actions.

Why we need to use JavascriptExecutor ?
Some time browser fails to load your script so in that case we have to execute JavaScript intentionally and perform our intended action.There is one important  method available in this interface i.e. 

executeScript(java.lang.String script, java.lang.Object... args)
With 2 parameters script and argument. We need to import below package to use the same :

org.openqa.selenium.JavascriptExecutor;

Lets start with the scenarios :

1. How to change attribute value of an element ?

Let's say i have below button which is hidden I need perform to click on but selenium detect it as Invisible.

<button id="datasubmit" name="btndubmit" style="display:none;">data submit</button>
If I try to locate, selenium will throw me the ElementNotVisibleException. So here you can set or change the attribute value of any element and perform your intended action. Here I'm going to change attribute value
WebElement element = driver.findElement(By.id("datasubmit"));

JavascriptExecutor js = (JavascriptExecutor)driver;

js.executeScript("arguments[0].style.display = 'block'", element );
Another scenario can be suppose I have some same kind of ad images on my page i want to hide all them, refer below tag which contains the ad


<img src="images/temp/advertisement.png">
Solution 1 :
JavascriptExecutor js = (JavascriptExecutor)driver;

List<WebElement> element = driver.findElements(By.tagName("img"));

for(WebElement e:element)

{
    if(e.getAttribute("src").contains("images/krishna_ads/jobs.png"))
    {
        js.executeScript("arguments[0].style.display = 'none'", e);

    }
}
Solution 2 :


    "imgs[i].setAttribute('style', 'display: none');" +

    " }" +

    "}" );
 2. How to enter text in a text field without using `sendKeys()` method of Selenium
WebElement element = driver.findElement(By.xpath(".//*[@id='example']/div[1]/div[2]/span/span/input"));

JavascriptExecutor js = (JavascriptExecutor)driver;

jse.executeScript("arguments[0].value='Aniseed Syrup';", element);
3. How to get bonded value of an element ?
WebElement element = driver.findElement(By.xpath(".//*[@id='example']/div[1]/div[2]/span/span/input"));

JavascriptExecutor jse = (JavascriptExecutor)driver;

String value = (String) jse.executeScript("return arguments[0].value", element) ;
4. How to evaluate an xpath which locating a text element ?
Selenium doesn't allow us to locate an element using text node. For example I have below HTML snippet
<table id="test" width="100">
     <tbody>
         <tr>
           <td valign="top" nowrap="">Details:</td>
             <th align="left" valign="top" >
                 Firsname : narendra<br>
                 Lastname: rajput<br>
                 Tool: selenium
              </th>
          </tr>
      </tbody>
</table>
Here I want to select the text "Firsname : narendra" only and I have the below xpath for that :
//table[@id='test']//th/text()[1]
but while use the same xpath in selenium to get the text it gives Exception xpath is not valid because selenium doesn't allow to locate the element using text node. So by using JavascriptExecutor you will be able to evaluate the same xpath. you have to use evaluate() method in following manner

Solution 1:
JavascriptExecutor js = (JavascriptExecutor)driver;

Object firstname= js.executeScript("var value = document.evaluate(\"//table[@id='test']//th/text()[1]\",document, null, XPathResult.STRING_TYPE, null ); return value.stringValue;");

System.out.println(firstname.toString());
Solution 2:
WebElement element = driver.findElement(By.xpath("//table[@id='test']//th"));

JavascriptExecutor js = (JavascriptExecutor)driver;

String firstname= (String) js.executeScript("return arguments[0].childNodes[0].textContent", element);
Apart from these you can do the following things too -

5. To perform the click on an element if click() method of Selenium is not working
WebElement element = driver.findElement(By.yourlocator);

JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript("arguments[0].click();", element);

6. Generate Alert Pop window 
JavascriptExecutor js = (JavascriptExecutor)driver;

Js.executeScript("alert('Javascript alert');");

7. Refresh the page 
JavascriptExecutor js = (JavascriptExecutor)driver;

driver.executeScript("history.go(0)");

8. Get innertext of the entire webpage
JavascriptExecutor js = (JavascriptExecutor)driver;
String innertext =  js.executeScript("return document.documentElement.innerText;").toString();

9. Get the Title of our webpage
JavascriptExecutor js = (JavascriptExecutor)driver;
String pagetitle =  js.executeScript("return document.title;").toString();

10. Perform Scroll on application
JavascriptExecutor js = (JavascriptExecutor)driver;

js.executeScript("window.scrollBy(0,50)");