Showing posts with label Selenium. Show all posts
Showing posts with label Selenium. Show all posts

Tuesday, March 8, 2016

EASY TO RUN APPIUM TESTS ON REAL ANDROID DEVICE WITHOUT CONNECTING TO PC USING USB PORT [TESTS RUNS REMOTELY THROUGH WI-FI]

We have to make sure that Android Device is connected with ADB over wi-fi to Run Appium Tests remotely on real Android device.


Steps:

1. Connect device to computer via USB.
2. Make sure device and computer are connected to the same Wi-Fi.
3. Run this command to restart adb and make it work over tcpip:
adb tcpip 5555
4. Disconnect device
5. Get IP address of your phone
6. Run this command to connect adb to your device over Wi-Fi using IP address:
adb connect <Phone IP Address>
7. Verify, that adb works remotely:
adb devices
8. Run tests over Wi-Fi.


Happy Automation !! 

Wednesday, July 1, 2015

SELENIUM : WAITING FOR CONDITIONS TO BE AVAILABLE


While testing a very slow loading website, the dreaded "NoSuchElementException" exception can occur even though the element does exist on the page. The problem can be that the code is running faster than the site loads. The "ExpectedCondition" class ensures that the program waits for a designated period of time before throwing the "NoSuchElementException" error message.


In the code below, we navigate to craigslist, take 5 seconds to wait for the required element to appear and be ready for clicking. Then click the element.

WebDriver driver = new FirefoxDriver();

// a slow loading website is good for this text. craigslist is not slow though :)

driver.get("http://london.craigslist.co.uk/");  

// define timeout for waiting period

WebDriverWait wait = new WebDriverWait(driver, 5);    

// ensures the element is available for click. Check for the defined 5 seconds before timeout

WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='jjj']/h4/a")));   

element.click();

SELENIUM : READ BROWSER CONSOLE LOG


Initialize driver with logging preferences.




DesiredCapabilities caps = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, Level.ALL);
caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
driver = new ChromeDriver(caps);
Now analyze log using below code:
LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);

for (LogEntry entry : logEntries) {
System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage());
//do something useful with the data
}

Tuesday, June 30, 2015

SELENIUM : RERUN FAILED TEST SCRIPTS USING TESTNG


There will be situations where we will re run failed automation test scripts and get the positive results upon rerun.


What if we have some utility which will rerun each failed test script..?


Yes.., We have it..we can automate rerunning test scripts scenario using IRetryAnalyzer and IRetryAnalyzer  interfaces of TestNG.


Create a Java class say RetryTest which implements IRetryAnalyzer  interface of TestNG.


Here, retryDemo() is my test method which will reexecuted on failure.
We are overriding retry() method of IRetryAnalyzer interface.
We are overriding transform() method of IAnnotationTransformer interface.


import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.IAnnotationTransformer;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
import org.testng.annotations.ITestAnnotation;
import org.testng.annotations.Test;

 public class RetryTest implements IRetryAnalyzer,IAnnotationTransformer {

     private int retryCount = 0;
    private int maxRetryCount = 1; //Mention how many times we want to rerun the failed test case.
   
    @Override
    public boolean retry(ITestResult result) {
     if (retryCount &lt; maxRetryCount) {
      System.out.println("Retrying test " + result.getName()
        + " with status " + getResultStatusName(result.getStatus())
        + " for the " + (retryCount + 1) + " time(s).");
      retryCount++;
      return true;
     }
     return false;
    }
   

     @Override
    public void transform(ITestAnnotation testannotation, Class testClass,
      Constructor testConstructor, Method testMethod)    {
     IRetryAnalyzer retry = testannotation.getRetryAnalyzer();

      if (retry == null)    {
      testannotation.setRetryAnalyzer(AutomationFrameWork.class);
     }

     }
   
   
    @Test(retryAnalyzer = RetryTest.class)
    public void retryDemo() throws Exception {
     WebDriver driver = new FirefoxDriver();
     driver.get("https://www.google.co.in");
     WebElement searchBtn = driver.findElement(By.xpath("//input[@name='btnK']"));//get webelement of search button on Google Home Page.
     String actual = searchBtn.getAttribute("value");//Value: Google Search
     String expected = "Yahoo";
     Assert.assertEquals(actual, expected);
   
    }
   
    public String getResultStatusName(int status) {
     String resultName = null;
     if (status == 1)
      resultName = "SUCCESS";
     if (status == 2)
      resultName = "FAILURE";
     if (status == 3)
      resultName = "SKIP";
     return resultName;
    }

   

 }


Thats all folks…!


You have to just modify the test method: retryDemo() based upon need and enjoy.


Happy Coding !