Comparing two strings in Java

package javabasics;

public class compare2String
{

	public static void main(String[] args) 
	{
		
		String S1 = "India is my country";
		String S2 = "USA is my country";
		  
		//Comparing two strings. Return true If both match else return false
		System.out.println("S1 equals to S2? : --- "+S1.equals(S2)+"\n");  
		
	}

}

Output:

S1 equals to S2? : --- false

 

Write and Read from Text File in JAVA

package javabasics;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ReadAndWrite {

	public static void main(String[] args) throws IOException {
		
		String TestFile = "E:\\Selenium\\notePadfile.txt";

		File fc = new File(TestFile);

		fc.createNewFile();

		  
		//Writing In to file.
		
		FileWriter fw = new FileWriter(TestFile);

		BufferedWriter bw = new BufferedWriter(fw);

		bw.write("This Is First Line."); //Writing In To File.

		bw.newLine();//To write next string on new line.

		bw.write("This Is Second Line."); //Writing In To File.

		bw.close();
		  
		//Reading from file.
		
		FileReader fr = new FileReader(TestFile);

		BufferedReader br = new BufferedReader(fr);

		String Content = "";
		
		//Loop to read all lines one by one from file and print It.

		while((Content = br.readLine())!= null){

		System.out.println(Content);

		}

	}

}

 

Use Grouping in TestNG – Selenium Webdriver

package testng;

import org.testng.annotations.Test;

public class Grouping 
{
	@Test (groups={"regression"})
	public void testcase1()
	{
		System.out.println("It's Test case 1 and in regression group" +"\n");
	}
	@Test (groups={"unit"})
	public void testcase2()
	{
		System.out.println("It's Test case 2 and in unit group" +"\n");
	}
	@Test (groups={"regression"})
	public void testcase3()
	{
		System.out.println("It's Test case 3 and in regression group" +"\n");
	}

}

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Grouping Test Case Suite">
  <test name="grouping">
  	<groups>
  	<define name = "all">
  		<include name="regression"/>
  		<exclude name="unit"/>
  	</define>
      <run>
        <include name="all"/>
      </run>
    </groups>
    <classes>
       <class name="testng.Grouping" />
    </classes>
  </test>
</suite>

 

Using Priority Test Case in TestNG

Test Case: 1

package testng;

import org.testng.annotations.Test;

public class PriorityTestCase 
{
	@Test (priority=1)
	public void register()
	{
		System.out.println("Create an account in Gmail" +"\n");
	}
	@Test (priority=2)
	public void login()
	{
		System.out.println("Login into an account in Gmail" +"\n");
	}
	@Test (priority=3)
	public void logout()
	{
		System.out.println("Logout from Gmail account" +"\n");
	}

}

Output 1:

[TestNG] Running:  
  C:\Users\QA\AppData\Local\Temp\testng-eclipse-241244459\testng-customsuite.xml

Create an account in Gmail

Login into an account in Gmail

Logout from Gmail account

PASSED: register
PASSED: login
PASSED: logout

===============================================
    Default test
    Tests run: 3, Failures: 0, Skips: 0
===============================================

Test Case 2:

package testng;

import org.testng.annotations.Test;

public class PriorityTestCase 
{
	@Test (priority=3)
	public void register()
	{
		System.out.println("Create an account in Gmail" +"\n");
	}
	@Test (priority=2)
	public void login()
	{
		System.out.println("Login into an account in Gmail" +"\n");
	}
	@Test (priority=1)
	public void logout()
	{
		System.out.println("Logout from Gmail account" +"\n");
	}

}

Output 2:

[TestNG] Running:
  C:\Users\QA\AppData\Local\Temp\testng-eclipse--1702558414\testng-customsuite.xml

Logout from Gmail account

Login into an account in Gmail

Create an account in Gmail

PASSED: logout
PASSED: login
PASSED: register

===============================================
    Default test
    Tests run: 3, Failures: 0, Skips: 0
===============================================

 

Include and Exclude Methods in TestNG

package testng;

import org.testng.annotations.Test;

public class IncludeandExclude 
{
	@Test
	public void TestCase1()
	{
		System.out.println("This is my 1st test case."+"\n");
	}
	
	@Test
	public void TestCase2()
	{
		System.out.println("This is my 2nd test case."+"\n");
	}
	
	@Test
	public void TestCase3()
	{
		System.out.println("This is my 3rd test case."+"\n");
	}

}

testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Include and Exclude Test Suite" verbose="1" >
  <test name="Method Test Cases" >
    <classes>
       <class name="testng.IncludeandExclude">
        <methods>
        <include name="TestCase1" />
        <include name="TestCase2" />
        <exclude name="TestCase3" /> 
      </methods>
       </class>
    </classes>
  </test>
</suite>

Output

[TestNG] Running:
  E:\Selenium\Myscripts\testng.xml

This is my 1st test case.

This is my 2nd test case.


===============================================
Include and Exclude Test Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

 

Passing Parameters through testng.xml

Test Case:

package testng;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class PassingParameters 
{
	@Parameters({"username", "password"})
	
	@Test
	public void login (String username, String password)
	{
		System.out.println("User name :- " + username +"\n");
		
		System.out.println("Password :- " + password +"\n");
	}	

}

testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Passing parameters Test Suite" >
  <test name="Parameterization Execution"  >
  <parameter name="username" value="selenium"/>
  <parameter name="password" value="testng123"/>
    <classes>
      <class name="testng.PassingParameters"/>      
    </classes>
  </test>
</suite>

Output:

[TestNG] Running:
  E:\Selenium\Myscripts\testng.xml

User name :- selenium

Password :- testng123


===============================================
Passing parameters Test Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

 

1 2 3 4 5 14