Showing posts with label testing. Show all posts
Showing posts with label testing. Show all posts

Tuesday, March 12, 2019

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

Select an Item from the drop-down in selenium webdriver

 package learning;  
 import java.util.concurrent.TimeUnit;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.chrome.ChromeDriver;  
 import org.openqa.selenium.support.ui.Select;  
 public class Dropdown {  
      public static void main(String[] args) {  
           System.setProperty("webdriver.chrome.driver", "C:\\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://html.com/attributes/select-size/?utm_source=network%20recirc&utm_medium=Bibblio");  
           //Select dropdown by Select class  
           Select select = new Select(driver.findElement(By.xpath("//select")));  
           //select required item from the above dropdown  
           select.selectByValue("Lesser");  
      }  
 }  

Dealing with modal dialog in selenium Webdriver

 package test.test;  
 import java.util.concurrent.TimeUnit;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.JavascriptExecutor;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.chrome.ChromeDriver;  
 public class ModelDialog {  
      public static void main(String[] args) throws InterruptedException {  
           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://html.com/input-type-file/");  
           WebElement element = driver.findElement(By.xpath("//*[@id='fileupload']"));  
           JavascriptExecutor jse = (JavascriptExecutor) driver;  
           jse.executeScript("arguments[0].scrollIntoView()", element);  
           element.sendKeys("C:/Users/Mutturaj/Downloads/chromedriver_win32/chromedriver.exe");  
           System.out.println("File Uploaded");  
           // activeElement() method clicks the modal dialog  
           driver.switchTo().activeElement();  
           // Click on the button, here in out case we click on the 'Don't allow'  
           // button  
           driver.findElement(By.linkText("Don't Allow")).click();  
           driver.findElement(By.xpath("//input[@type='submit' and @value='submit']")).click();  
           System.out.println("Submitted");  
      }  
 }  

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");  
            */  
      }  
 }  

Implicit wait in Selenium

 package learning;  
 import java.util.concurrent.TimeUnit;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.chrome.ChromeDriver;  
 import org.openqa.selenium.interactions.Actions;  
 import org.openqa.selenium.support.ui.ExpectedConditions;  
 import org.openqa.selenium.support.ui.WebDriverWait;  
 public class MoveMouse {  
      public static void main(String[] args) throws Exception {  
           System.setProperty("webdriver.chrome.driver", "C:\\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://www.spicejet.com");  
           Actions actions = new Actions(driver);  
           actions.moveToElement(driver.findElement(By.linkText("ADD-ONS"))).build().perform();  
           Thread.sleep(3000);  
           waitForVisa(driver,driver.findElement(By.linkText("Dubai Visa Services")),20);  
           driver.close();  
      }  
//Implicit wait method
      public static void waitForVisa(WebDriver driver, WebElement element,int timeout){  
           new WebDriverWait(driver,timeout).until(ExpectedConditions.elementToBeClickable(element));  
           element.click();            
      }  
 }