Monday, April 13, 2020

About main() method in java

public static void main(String args[]){
}

Why 

  1. public  ----- so that jvm can get control of main method from anywhere
  2. static ---- so that main method could be accessed without any object reference
  3. void .... main method does not return value
  4. String args[]  ---- when you are executing jar file with java class with main method in it. we can pass String arguments to main method if needed.

Sunday, April 12, 2020

return object

Do you know we can return object in Java, for example, if you want to use WebDriver object created in a method  in someother method in someother class, you can return that object.
Syntax:

Public  Classname  methodname(){
Classname obj= new Classname();
return  obj;
}

The above example tells us how to create method to return an object.  Below is live example of WebDriver object:

Public  WebDriver  methodname(){
WebDriver  object=  new  ChromeDriver();
return  object;
}

When you call above method  in other class  like:
WebDriver object1=  methodname();

object1.url("https://www.google.com");


It will still trigger google website.

Sunday, November 24, 2019

Interview

11/23/19:

Interview:
- Arraylist add values
- Excel read values
- downloading file
- uploading file
- update Excel
- imp of maven other than adding dependencies
- imp testng and jun,rall time from two strings with time mentioned in it as strings in it.
- soapui..tell hw to test
- shell and python
- different error codes in services and
- use testng rather than run manager excel to control script execution
- plugins in eclipse

Friday, November 1, 2019

My Misconceptions on Java


  1. 1. To.make use of extended methods from a class and implement methods from interface , you need not always use 'extend' or 'implements' . A individual class can still use these methods by importing respective classes (For interface importing respective class that has implemented interface methods should suffice).
  2. DesiredCapabilities dc = DesiredCapabilities.chrome();   -- what does this do
  3. FruitClass a = new JamunClass();  ... This help to refer to methods of JamunClass with the help of object 
          Similarly we can use:
         Interfacename obj= new classname()
         This is generally used when some other class(used above code inside it) which has just imported the class that implements interface
4. UserClass a1 = new UserClass(FruitClass obj);  -- what does obj do here

Sunday, March 4, 2018

Debug (line by line) java programming


 - Bug icon on the top will take you to debug mode
- step into, step over and step return also exist in the eclipse, once you in debug mode.
Once you click Bug icon , you will see 3 icons on the top in the menu, which will help you to do the activities.


Procedure 2:
1. Add breakpoint
2. Select Debug As Java Application 
3. Eclipse ide will pop up a message that debug has been initiated
4. Script will be executing till the break point. It will come and stop at break point line.
5. Here you could use F5,F6,F7*  to take control of the movement of code. 
6. In debug section you will see outcome of each line of code starting from break point.
7. You have resume in menu bar just to come out of debug mode.

Friday, January 15, 2016

Objects - Passing as parameters in methods, returning objects

package supportlibraries;
import org.openqa.selenium.WebDriver;
import com.cognizant.framework.CraftDataTable;
import com.cognizant.framework.selenium.SeleniumReport;


/**
 * Wrapper class for common framework objects, to be used across the entire test case and dependent libraries
 * @author Cognizant
 */
public class ScriptHelper
{
private final CraftDataTable dataTable;
private final SeleniumReport report;
private final WebDriver driver;

/**
* Constructor to initialize all the objects wrapped by the {@link ScriptHelper} class
* @param dataTable The {@link CraftDataTable} object
* @param report The {@link SeleniumReport} object
* @param driver The {@link WebDriver} object
*/
public ScriptHelper(CraftDataTable dataTable, SeleniumReport report, WebDriver driver)
{
this.dataTable = dataTable;
this.report = report;
this.driver = driver;
}

/**
* Function to get the {@link CraftDataTable} object
* @return The {@link CraftDataTable} object
*/
public CraftDataTable getDataTable()
{
return dataTable;
}

/**
* Function to get the {@link SeleniumReport} object
* @return The {@link SeleniumReport} object
*/
public SeleniumReport getReport()
{
return report;
}

/**
* Function to get the {@link WebDriver} object
* @return The {@link WebDriver} object
*/
public WebDriver getDriver()
{
return driver;
}
}

Wednesday, January 13, 2016

Regular Expression



1/12/2016
source: http://www.tutorialspoint.com/java/java_regular_expressions.htm

Java provides the java.util.regex package for pattern matching with regular expressions. Java regular expressions are very similar to the Perl programming language and very easy to learn.
A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. They can be used to search, edit, or manipulate text and data.
The java.util.regex package primarily consists of the following three classes:
  • Pattern Class: A Pattern object is a compiled representation of a regular expression. The Pattern class provides no public constructors. To create a pattern, you must first invoke one of its public staticcompile() methods, which will then return a Pattern object. These methods accept a regular expression as the first argument.
  • Matcher Class: A Matcher object is the engine that interprets the pattern and performs match operations against an input string. Like the Pattern class, Matcher defines no public constructors. You obtain a Matcher object by invoking the matcher() method on a Pattern object.
  • PatternSyntaxException: A PatternSyntaxException object is an unchecked exception that indicates a syntax error in a regular expression pattern.

Capturing Groups:

Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d", "o", and "g".
Capturing groups are numbered by counting their opening parentheses from left to right. In the expression ((A)(B(C))), for example, there are four such groups:
  • ((A)(B(C)))
  • (A)
  • (B(C))
  • (C)
To find out how many groups are present in the expression, call the groupCount method on a matcher object. The groupCount method returns an int showing the number of capturing groups present in the matcher's pattern.
There is also a special group, group 0, which always represents the entire expression. This group is not included in the total reported by groupCount.

Example:

Following example illustrates how to find a digit string from the given alphanumeric string:
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches
{
    public static void main( String args[] ){

      // String to be scanned to find the pattern.
      String line = "This order was placed for QT3000! OK?";
      String pattern = "(.*)(\\d+)(.*)";

      // Create a Pattern object
      Pattern r = Pattern.compile(pattern);

      // Now create matcher object.
      Matcher m = r.matcher(line);
      if (m.find( )) {
         System.out.println("Found value: " + m.group(0) );
         System.out.println("Found value: " + m.group(1) );
         System.out.println("Found value: " + m.group(2) );
      } else {
         System.out.println("NO MATCH");
      }
   }
}
This would produce the following result:
Found value: This order was placed for QT3000! OK?
Found value: This order was placed for QT300
Found value: 0

Regular Expression Syntax:

Here is the table listing down all the regular expression metacharacter syntax available in Java:
SubexpressionMatches
^Matches beginning of line.
$Matches end of line.
.Matches any single character except newline. Using m option allows it to match newline as well.
[...]Matches any single character in brackets.
[^...]Matches any single character not in brackets
\ABeginning of entire string
\zEnd of entire string
\ZEnd of entire string except allowable final line terminator.
re*Matches 0 or more occurrences of preceding expression.
re+Matches 1 or more of the previous thing
re?Matches 0 or 1 occurrence of preceding expression.
re{ n}Matches exactly n number of occurrences of preceding expression.
re{ n,}Matches n or more occurrences of preceding expression.
re{ n, m}Matches at least n and at most m occurrences of preceding expression.
a| bMatches either a or b.
(re)Groups regular expressions and remembers matched text.
(?: re)Groups regular expressions without remembering matched text.
(?> re)Matches independent pattern without backtracking.
\wMatches word characters.
\WMatches nonword characters.
\sMatches whitespace. Equivalent to [\t\n\r\f].
\SMatches nonwhitespace.
\dMatches digits. Equivalent to [0-9].
\DMatches nondigits.
\AMatches beginning of string.
\ZMatches end of string. If a newline exists, it matches just before newline.
\zMatches end of string.
\GMatches point where last match finished.
\nBack-reference to capture group number "n"
\bMatches word boundaries when outside brackets. Matches backspace (0x08) when inside brackets.
\BMatches nonword boundaries.
\n, \t, etc.Matches newlines, carriage returns, tabs, etc.
\QEscape (quote) all characters up to \E
\EEnds quoting begun with \Q

Methods of the Matcher Class:

Here is a list of useful instance methods:

Index Methods:

Index methods provide useful index values that show precisely where the match was found in the input string:
SNMethods with Description
1public int start()
Returns the start index of the previous match.
2public int start(int group)
Returns the start index of the subsequence captured by the given group during the previous match operation.
3public int end()
Returns the offset after the last character matched.
4public int end(int group)
Returns the offset after the last character of the subsequence captured by the given group during the previous match operation.

Study Methods:

Study methods review the input string and return a Boolean indicating whether or not the pattern is found:
SNMethods with Description
1public boolean lookingAt()
Attempts to match the input sequence, starting at the beginning of the region, against the pattern.
2public boolean find()
Attempts to find the next subsequence of the input sequence that matches the pattern.
3public boolean find(int start)
Resets this matcher and then attempts to find the next subsequence of the input sequence that matches the pattern, starting at the specified index.
4public boolean matches()
Attempts to match the entire region against the pattern.

Replacement Methods:

Replacement methods are useful methods for replacing text in an input string:
SNMethods with Description
1public Matcher appendReplacement(StringBuffer sb, String replacement)
Implements a non-terminal append-and-replace step.
2public StringBuffer appendTail(StringBuffer sb)
Implements a terminal append-and-replace step.
3public String replaceAll(String replacement)
Replaces every subsequence of the input sequence that matches the pattern with the given replacement string.
4public String replaceFirst(String replacement)
Replaces the first subsequence of the input sequence that matches the pattern with the given replacement string.
5public static String quoteReplacement(String s)
Returns a literal replacement String for the specified String. This method produces a String that will work as a literal replacement s in the appendReplacement method of the Matcher class.

The start and end Methods:

Following is the example that counts the number of times the word "cat" appears in the input string:
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches
{
    private static final String REGEX = "\\bcat\\b";
    private static final String INPUT =
                                    "cat cat cat cattie cat";

    public static void main( String args[] ){
       Pattern p = Pattern.compile(REGEX);
       Matcher m = p.matcher(INPUT); // get a matcher object
       int count = 0;

       while(m.find()) {
         count++;
         System.out.println("Match number "+count);
         System.out.println("start(): "+m.start());
         System.out.println("end(): "+m.end());
      }
   }
}
This would produce the following result:
atch number 1
start(): 0
end(): 3
atch number 2
start(): 4
end(): 7
atch number 3
start(): 8
end(): 11
atch number 4
start(): 19
end(): 22
You can see that this example uses word boundaries to ensure that the letters "c" "a" "t" are not merely a substring in a longer word. It also gives some useful information about where in the input string the match has occurred.
The start method returns the start index of the subsequence captured by the given group during the previous match operation, and end returns the index of the last character matched, plus one.

The matches and lookingAt Methods:

The matches and lookingAt methods both attempt to match an input sequence against a pattern. The difference, however, is that matches requires the entire input sequence to be matched, while lookingAt does not.
Both methods always start at the beginning of the input string. Here is the example explaining the functionality:
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches
{
    private static final String REGEX = "foo";
    private static final String INPUT = "fooooooooooooooooo";
    private static Pattern pattern;
    private static Matcher matcher;

    public static void main( String args[] ){
       pattern = Pattern.compile(REGEX);
       matcher = pattern.matcher(INPUT);

       System.out.println("Current REGEX is: "+REGEX);
       System.out.println("Current INPUT is: "+INPUT);

       System.out.println("lookingAt(): "+matcher.lookingAt());
       System.out.println("matches(): "+matcher.matches());
   }
}
This would produce the following result:
Current REGEX is: foo
Current INPUT is: fooooooooooooooooo
lookingAt(): true
matches(): false

The replaceFirst and replaceAll Methods:

The replaceFirst and replaceAll methods replace text that matches a given regular expression. As their names indicate, replaceFirst replaces the first occurrence, and replaceAll replaces all occurrences.
Here is the example explaining the functionality:
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches
{
    private static String REGEX = "dog";
    private static String INPUT = "The dog says meow. " +
                                    "All dogs say meow.";
    private static String REPLACE = "cat";

    public static void main(String[] args) {
       Pattern p = Pattern.compile(REGEX);
       // get a matcher object
       Matcher m = p.matcher(INPUT); 
       INPUT = m.replaceAll(REPLACE);
       System.out.println(INPUT);
   }
}
This would produce the following result:
The cat says meow. All cats say meow.

The appendReplacement and appendTail Methods:

The Matcher class also provides appendReplacement and appendTail methods for text replacement.
Here is the example explaining the functionality:
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches
{
   private static String REGEX = "a*b";
   private static String INPUT = "aabfooaabfooabfoob";
   private static String REPLACE = "-";
   public static void main(String[] args) {
      Pattern p = Pattern.compile(REGEX);
      // get a matcher object
      Matcher m = p.matcher(INPUT);
      StringBuffer sb = new StringBuffer();
      while(m.find()){
         m.appendReplacement(sb,REPLACE);
      }
      m.appendTail(sb);
      System.out.println(sb.toString());
   }
}
This would produce the following result:
-foo-foo-foo-

PatternSyntaxException Class Methods:

A PatternSyntaxException is an unchecked exception that indicates a syntax error in a regular expression pattern. The PatternSyntaxException class provides the following methods to help you determine what went wrong:
SNMethods with Description
1public String getDescription()
Retrieves the description of the error.
2public int getIndex()
Retrieves the error index.
3public String getPattern()
Retrieves the erroneous regular expression pattern.
4public String getMessage()
Returns a multi-line string containing the description of the syntax error and its index, the erroneous regular expression pattern, and a visual indication of the error index within the pattern.