Showing posts with label core java. Show all posts
Showing posts with label core java. Show all posts

Tuesday, March 12, 2019

Data provider in TestNG

 package muttu.test;  
 import org.testng.annotations.DataProvider;  
 import org.testng.annotations.Test;  
 public class DataProviderDemo {  
      @DataProvider(name = "LoginDeatls")  
      public Object[][] getUserDetails() {  
           return new Object[][] { { "uName1", "pwd1" }, { "uName2", "pwd2" } };  
      }  
      @Test(dataProvider = "LoginDeatls")  
      public void Login(String name, String password) {  
           System.out.println(name);  
           System.out.println(password);  
      }  
 }  

Run failed test cases in Java


Create a class that fails test case


 package testNG;  
 import org.testng.Assert;  
 import org.testng.annotations.Test;  
 public class TestFailClass {  
      @Test(retryAnalyzer=MyRetry.class)  
      public void failMethod(){  
           System.out.println("FAILED CLASS");  
           Assert.assertTrue(false);  
      }  
 }  

Create a class that re-runs the failed test cases

 package testNG;  
 import java.util.ArrayList;  
 import java.util.List;  
 import org.testng.TestNG;  
 public class RunFailed {  
      public static void main(String[] args) {  
           TestNG testRunner = new TestNG();  
           List<String> failedXmls = new ArrayList<String>();  
           //Enter the full path for the 'test-faailed.xml' file  
           failedXmls.add("C:/Users/Mutturaj.h/workspace/hulagabal/test-output/testng-failed.xml");  
           testRunner.setTestSuites(failedXmls);  
           testRunner.run();  
      }  
 }  

Retry failed test cases TestNG in Java


Create test cases which fails

 package testNG;  
 import org.testng.Assert;  
 import org.testng.annotations.Test;  
 public class TestFailClass {  
      @Test(retryAnalyzer=MyRetry.class)  
      public void failMethod(){  
           Assert.fail("Test failed");  
      }  
 }  

Create a retry class which implements IRetryAnalyzer interface like below which tries for 3 times

 package testNG;  
 import org.testng.IRetryAnalyzer;  
 import org.testng.ITestResult;  
 public class MyRetry implements IRetryAnalyzer {  
      private int retryCount = 0;  
      private static final int maxRetryCount = 3;  
      public boolean retry(ITestResult result) {  
           if (retryCount < maxRetryCount) {  
                retryCount++;  
                return true;  
           }  
           return false;  
      }  
 }  

Friday, March 8, 2019

Handling Alert in selenium

 import java.util.concurrent.TimeUnit;  
 import org.openqa.selenium.Alert;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
 public class AlertPopup {  
      public static void main(String[] args) {  
           System.setProperty("webdriver.chrome.driver",  
                     "C:\\Users\\Mutturaj\\Downloads\\chromedriver_win32\\chromedriver.exe");  
           WebDriver driver = new ChromeDriver();  
           driver.manage().window().maximize();  
           driver.manage().deleteAllCookies();  
           driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);  
           driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);  
           driver.get("https://mail.rediff.com/cgi-bin/login.cgi");  
           driver.findElement(By.name("proceed")).click();  
           Alert alert = driver.switchTo().alert();  
           System.out.println(alert.getText());  
           alert.accept();  

           // other importent methods  
           /*  
            * alert.getText() alert.dismiss(); alert.sendKeys("text");  
            */  
      }  
 }  

Monday, February 11, 2019

Access Levels in Java



Modifier
Class
Package
Subclass
World
public
 Y
Y
Y
Y
protected
Y
Y
Y
N
no modifier
Y
Y
N
N
private
Y
N
N
N
*no modifier is: Package private

Saturday, February 9, 2019

String Manipulations


 public class Str {  
  public static void main(String[] args) {  
  String s = "Mutturaj Hulagabal";  
  System.out.println(s.charAt(4));  
  System.out.println(s.length());  
  String trim = s.trim();  
  System.out.println(trim.length());  
  System.out.println(s.concat("Atharga"));  
  System.out.println(s.contains("Muttu"));  
  System.out.println(s.indexOf("H"));  
  System.out.println(s.compareTo("Naveen Patil"));  
  System.out.println(s.toUpperCase() + " " + s.toLowerCase());  
  System.out.println(s.compareTo("Mutturaj Hulagabal"));  
  System.out.println(s.compareToIgnoreCase("mutturaj hulagabal"));  
  System.out.println(s.isEmpty());  
  // Sub String  
  System.out.println(s.substring(9));  
  System.out.println(s.substring(0, 8));  
  // Object  
  System.out.println(s.equals("Mutturaj Hulagabal"));  
  }  
 }