Friday, 12 April 2019

Java String format() with examples

// Java program to demonstrate
// working of format() method

class Gfg1 {
public static void main(String args[])
{
String str = "GeeksforGeeks.";

// Concatenation of two strings
String gfg1 = String.format("My Company name is %s", str);

// Output is given upto 8 decimal places
String str2 = String.format("My answer is %.8f", 47.65734);

// between "My answer is" and "47.65734000" there are 15 spaces
String str3 = String.format("My answer is %15.8f", 47.65734);

System.out.println(gfg1);
System.out.println(str2);
System.out.println(str3);
}
}



class Gfg2 {
    public static void main(String args[])
    {
        String str1 = "GFG";
        String str2 = "GeeksforGeeks";
  
        //%1$ represents first argument, %2$ second argument
        String gfg2 = String.format("My Company name"
                 " is: %1$s, %1$s and %2$s", str1, str2);
  
        System.out.println(gfg2);
    }
}
Output:
My Company name is: GFG, GFG and GeeksforGeeks

// Java program to show
// left padding using
// format() method
  
class Gfg3 {
    public static void main(String args[])
    {
        int num = 7044;
  
        // Output is 3 zero's("000") + "7044",
        // in total 7 digits
        String gfg3 = String.format("%07d", num);
  
        System.out.println(gfg3);
    }
}
Output:
0007044


Java Faker Class

pom.xml
 <dependencies>
  <dependency>
    <groupId>com.github.javafaker</groupId>
    <artifactId>javafaker</artifactId>
    <version>0.15</version>
</dependency>
  </dependencies>


Link :https://www.baeldung.com/java-faker


package com.fakerdata;
import com.github.javafaker.Faker;
public class FakerData {

public static void main(String[] args) {
Faker faker = new Faker();
String streetName = faker.address().streetName();
String number = faker.address().buildingNumber();
String city = faker.address().city();
String country = faker.address().country();
System.out.println(String.format("%s\n%s\n%s\n%s",
  number,
  streetName,
  city,
  country));
}

}

##OutPut###########################################################################
374
Kari Gardens
Metzport
Swaziland

Wednesday, 10 April 2019

TestNG Learnings

1)TestNG

#1 If we have 2 methods with no priority defined, execution happens by Alphabetical Order
Ex Add_Test() will get executed before  Div_Test() irespective of fact what order they have in script

#2 @Test(priority=1) is highest priority. it will get executed before @Test(priority=2)
* If two tests have @Test(priority=x) and third does not. Third is getting executed first


#3 @BeforeMethod executes before every method

#4 @AfterMethod executes after every  method

#5 @BeforeTest executes Once before number of tests.
ex. We want to create db connection before starting execution


#6AfterTest We would like to close opened connection. Will execute after all tests have been executed


#7 Assert should be used for reporting Failure or Pass. Without them its not possible to know results
   As soon as first assert fails test case is marked as failed. It wont got to next step.

**Executing with SoftAssert will shows only passes steps. test moves on to next step despite failure

SoftAssert sa=new SoftAssert();

sa.AssertEquals()  pass
sa.AssertEquals()  failed
sa.AssertEquals()  pass

=>Report will show only passed data


#8 If we want to shows failed steps only then we need to use at end of above script
Sa.assertAll


#9@Test(dependsonMethods="methodname")
This is hard depedency. It method with methodname fails, the current test will not get executed
Above setup adds data like: 1 Failed, 1 Skipped

Soft Depedency:
@Test(dependsonMethods="methodname",alwaysrun=true)

#9 When we write click on project, we get convert to testng as one of the option.
By defaults its adds all the classes in default testng.xml where we have used @Test annotation.


We can create multiple suites by adding removing classes and create a mster xml.
<suite name="MasterSuite">
<suite-files>
<suite-file path="testng.xml" />
<suite-file path="testng2.xml"/>
</suite-files>
</suite>


#10
We can give group to the test so that we can see them in report according to that.
One test can have multiple groups

Ex 1 @Test(priority=1,groups={"grp1","grp2"})
Ex 2 @Test(priority=1,groups="grp1")


#NOw if we want to run only cases belonging to that group then we can modify xml file as below

<suite name="Suite">
<run>
<include name="login_related"</include>
</run>
<test thread-count="5" name="Test">
<classes>

<class name="com.totaltestng.cases.TestBase" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

# We can also exclude other cases except this group by replacing include with exclude

# groups can be defined at suite level or at test level

<suite name="Suite">
<test thread-count="5" name="Test">
<groups>
<run>
<exclude name="loginPage"></exclude>
</run>
</groups>



#11 Listeners

Suppose we want to take a screenshot on failure

1. Create a new class  and implement ITestListner.
Add unimplemeted methods.

# in test failure method add yor code
# in respective testng file add
<listeners>
<listener class-name="com.totaltestng.listeners.TestngListener"/>
</listeners>


#12:
Forcefully skipping a test  case

$ We can use throw SkipException to skip the test

@Test
public void testToSkip() {

int i = 4;

if (i == 4) {

throw new SkipException("Skipping forcefully with SkipException");

}



SKIPPED: testToSkip
org.testng.SkipException: Skipping forcefully with SkipException





#12
Testng by default provides Reporter object


#13 <parameter name="browser" value="chrome"/>
  <parameter name="os" value="windows 7"/>\\


@Parameters({"browser","os"})
@Test
public void xmlPara(String browser, String os) {

System.out.println("BROWSER IS :"+browser+"  OS:"+os);


}




#14 Parallel test

Testng helps in paraller execution
Grid helps in distributing tests on remote machine

parallet="tests" on suite line


#15

We can use paralle with data provider

@DataProvider(parallel=true)



#16 Invocationcount=5

executes 5 time

Invocationcount=5,threadPoolSize=5
lauches browsers 5 time

^[0-9|\s]|[0-9|\s]$




chrome default profile
C:\Users\config\AppData\Local\Google\Chrome\User Data\Default




System.setProperty("webdriver.chrome.driver", "H:\\chromedriver (2).exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:\\Users\\config\\AppData\\Local\\Google\\Chrome\\User Data\\Default");
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
driver.get("https://www.zoho.com/");

Java: String and StringBuffer

package learningExtra;

public class StringandStringBuffer {


public static void main(String[] args) {
String st1=new String("sumit");
String st2=new String("Neha");
String st3=new String("sumit");
st1.concat("raut");
System.out.println(st1); //#########Prints sumit because String class is immutable

StringBuffer sb1=new StringBuffer("Amit");
StringBuffer sb2=new StringBuffer("Amit");
sb1.append("raut");
System.out.println(sb1); //#########Prints Amitraut because StringBuffer class is not immutable


// #Note only has following way StringBuffer sb=new StringBuffer("yourtext")
System.out.println("#####Below For String#######");
System.out.println(st1==st3);       //    ==>false
System.out.println(st1.equals(st3));// true


System.out.println("#####Below For StringBuffer#######");
System.out.println(sb1==sb2);       //====>false
System.out.println(sb1.equals(sb2));//====> false   .equals to in StringBuffer is not overridden for Content Comparison.
                                                     //Hence does address comparison


System.out.println("#####Below For compareTo#######");
String test1=new String("Software");
String test2=new String("Testing");
String test3=new String("Software");
System.out.println(test1.compareTo(test2)); //-1   unicode of test1<unicode of test2
System.out.println(test1.compareTo(test3)); //0   unicode of test1=test3
System.out.println(test2.compareTo(test1)); //1    unicode of test2>unicode of test1
}

}

Thursday, 4 April 2019

VbScript: Reversing String

Function strverese()
Dim name: name = "sumit"
Dim strRev: strRev = ""

strLength = Len(name)

For i = strLength To 1 Step -1

strRev = strRev + Mid(name, i, 1)

Next

MsgBox strRev

End Function

Vbscript: Fibonacci Logic

Function fibbonacci()

Dim t1: t1 = 0
Dim t2: t2 = 1

For i = 1 To 10 Step 1

MsgBox "" & t1
Sum = t1 + t2
t1 = t2
t2 = Sum

Next

End Function

Rest Assured Chectsheet

REST Assured Cheat Sheet 🚀 REST Assured Cheat Sheet Complete Reference for ...