Wednesday 18 November 2015

Create random numbers in java

Creating Random numbers in java can be generated in java using Random class.

Implementation is as below, which will create random integers starting from 0(inclusive) to 90(exclusive)

Random generator = new Random();
        ArrayList<Integer> numbers = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            numbers.add(generator.nextInt(90) );
        }


Example to create numbers within range (like between 15 and 105)

Random generator = new Random();
        ArrayList<Integer> numbers = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            numbers.add(generator.nextInt(90)+15 );
        }

Monday 7 September 2015

ERROR: The provided repository type was not detected in the current directory.

When creating diff using 'rbt post' for review board with perforce client, reported issue occurs as below. This can be solved by verifying the client root in p4client and the path from where the rbt post command issued must be the same.
 


C:\Users\demouser>rbt post -d --repository-type perforce

>>> RBTools 0.7.4

>>> Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)]

>>> Running on Windows-7-6.-SP1

>>> Home = C:\Users\demouser\AppData\Roaming

>>> Current directory = C:\Users\demouser

>>> Checking for a Perforce repository...

>>> Running: p4 info

ERROR: The provided repository type was not detected in the current directory.


 C:\Users\demouser>


Client root in p4 client should be the same as current directory

Saturday 11 July 2015

object doesn't support this property or method attachevent





  • When I try to launch the Internet Explorer 11 via Selenium RC server, faced with the below pop.
  • After surfing found attachEvent is a deprecated function used in older versions of Internet Explorer and modern browsers use addEventListener instead.
  • So if you are using earlier version of Selenium server try using the latest selenium standalone server. Which will solve the reported pop up issue.



couldn't open app window; is the pop-up blocker enabled

When I try to launch the Internet Explorer 11 via Selenium RC server, faced with the below pop.

After marking the pop up blocking to be turned off and disabling the “Enable protected mode settings” under security tab, the reported issues got resolved. Please find the helpful screen shots.





Saturday 16 May 2015

Unexpected error in launching a slave. This is probably a bug in Jenkins

Launching a slave agent in Jenkins could result in the below error. This occurs when adding a Linux machine as slave node in Jenkins. Here ssh connection to the Linux server fails.

[05/17/15 00:53:52] [SSH] Opening SSH connection to 192.168.212.18:22.

Permission denied: connect
ERROR: Unexpected error in launching a slave. This is probably a bug in Jenkins.
java.lang.IllegalStateException: Connection is not established!
    at com.trilead.ssh2.Connection.getRemainingAuthMethods(Connection.java:1030)
    at com.cloudbees.jenkins.plugins.sshcredentials.impl.TrileadSSHPasswordAuthenticator.canAuthenticate(TrileadSSHPasswordAuthenticator.java:82)
    at com.cloudbees.jenkins.plugins.sshcredentials.SSHAuthenticator.newInstance(SSHAuthenticator.java:207)
    at com.cloudbees.jenkins.plugins.sshcredentials.SSHAuthenticator.newInstance(SSHAuthenticator.java:169)
    at hudson.plugins.sshslaves.SSHLauncher.openConnection(SSHLauncher.java:1173)
    at hudson.plugins.sshslaves.SSHLauncher$2.call(SSHLauncher.java:701)
    at hudson.plugins.sshslaves.SSHLauncher$2.call(SSHLauncher.java:696)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
[05/17/15 00:53:53] Launch failed - cleaning up connection
[05/17/15 00:53:53] [SSH] Connection closed.




One instance where this could be corrected is to enable the publickey authentication in the linux server.

Few steps to do so are as below,


  • Edit the sshd_config file under /etc/ssh to modify the parameters, here we enable the publickey authentication and authorisedKeys.



  • After modifying the file to take effect restart sshd service



  • Then relaunch the slave agent which should be successful.












Creating slaves nodes of Linux and windows machines in Jenkins

Jenkins is a continuous integration tools for software development written in Java, where Builds can be started by  by commit in a version control system like git, svn  or by scheduling a time.
Builds can also be triggered after completing another build by having build dependencies by means of pre and post build actions.
Jenkins follows a master slave mechanism where no of slave nodes which are the actual clients where the builds are to be executed are added to the Jenkins masters. slave/masters can be running any operating system.
In this post we see how to add the Linux and windows nodes as slaves to the masters.
  1. From the Jenkins server navigate to the Manage Jenkins link on the left pane of the main page.
  2. which will take us to the control page of Jenkins where Manage Nodes link can be found.
  3. Now click the New Node link to create a new node
  4. Creating Linux node
  5. Enter the node name(Linux_Slave) and press OK
  6. Now click the configure link on the left pane as below and provide the configuration parameters as show in the screenshot.

  7. Here we provide the SSH login credentials to login to Linux machine and the path of JAVA_HOME has been given as a parameter.
  8. Save the node and verify the log console o the node.
  9.  Now the Slave node Linux_Slave is added to the jenkins master server. This node can be configure to use for any Jobs.

     
  10. Same process has to be followed to add a windows machine as slave to the Jenkins master.
  11.  The login method can be to use java web start as below
  12. Below picture shows the windows and Linux machine are added as slave nodes

Tuesday 24 March 2015

Running testng class file from cmd prompt and jenkins

we can run the testng class files through eclipse by adding the testng libraries in Build path.
If we need to run the same testng class files from command prompt and through any tools we could do so by adding the testng libraries to the CLASSPATH variable. or by giving the path with -cp option available in java. Also can add any jar files which are referred in the class files.



set CLASSPATH = %CLASSPATH%;D:\Are\Sel\Ecom_project\TestNG\testng-6.8.jar

D:\Area\Sel\>java org.testng.TestNG testng.xml
[TestNG] Running:
  D:\Are\Sel\Ecom_project\src\scripts\testng.xml

TestNG is working fine
===============================================
Suite1
Total tests run: 1, Failures: 0, Skips: 0
===============================================



To run the same testng scripts via Jenkins can add the CLASSPATH variable at master level or at node level specifying the path of the jar files. Represented the same in below snaps.










 

Friday 13 March 2015

Selenium grid implementation on two different machines and browsers


This post guides us for the GRID implementation of Selenium where we launch a grid server and register two nodes in two different machines.

Here we launch the HUB in machine1 and start node 1 with two instances of chrome browser and node 2 with two instances of Firefox.


To start Hub

HUB: Machine 1

java -jar selenium-server-standalone-2.43.1.jar -role hub -port 4455

To start nodes

Node : Machine 2 (launching only chrome browser of 2 instances)
 

java -jar selenium-server-standalone-2.43.1.jar -role node -hub http://<ip_of_hub_machine:4455>/grid/register -port 5557 -Dwebdriver.chrome.driver=chromedriver.exe -browser browserName=chrome,platform=WINDOWS,maxInstances=2

Node: Machine 3 (
launching only firefox browser of 2 instances)

java -jar selenium-server-standalone-2.45.0.jar -role node -hub http://
<ip_of_hub_machine:4455>/grid/register -port 5587 -browser browserName=firefox,platform=WINDOWS,maxInstances=2

After launching hub and nodes we can see the instances of the nodes in grid console under URL http://localhost:4455/grid/console

















 

Sample program:

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class GridTest {

    static DesiredCapabilities dc = null;
    WebDriver driver;

    public GridTest() throws MalformedURLException {
        dc = new DesiredCapabilities();
       

    }

    public void firefox() throws MalformedURLException, InterruptedException {
        dc.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
        excute("firefox");
    }

    public void chorme() throws MalformedURLException, InterruptedException {
        dc.setBrowserName(DesiredCapabilities.chrome().getBrowserName());
        excute("chrome");
    }

    public void excute(String browsername) throws MalformedURLException, InterruptedException {
       
        dc.setPlatform(Platform.WINDOWS);
        driver = new RemoteWebDriver(new URL(
                "http://<ip_of_hub_machine:4455>/wd/hub"), dc);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        System.out.println("In "+browsername);
        driver.get("https://www.google.co.in/");
        System.out.println(driver.getCurrentUrl());
        driver.quit();
    }

    public static void main(String[] args) throws MalformedURLException, InterruptedException {
        // TODO Auto-generated method stub

        GridTest t1 = new GridTest();

        t1.firefox();
        t1.chorme();

    }
}




Firefox in Machine 1

Chrome in Machine 2