Monday 20 February 2017

Exceptions In Selenium

Exceptions we get in Selenium::

No such element
Null pointer
Unreachable browser exception
class not found in classpath
timeoutexception
not visible exception
element not selectable exception
Stale element exception
no alert exception
invalid exception
WebDriver Exception

Monday 13 February 2017

Read Cell value From Excel.xlsx (Generic)

Reading Excel cell value::

Below code is taken based Database Reading.


public String  readData(String sheetname,String row,String column
{
try{
FileInputStream fis=new FileInputStream("./TestData.xlsx");
Workbook wb=WorkbookFactory.create(fis);
        Sheet sh=wb.getSheet(sheetname);
int noofrows=sh.getLastRowNum()+1;
int noofcolumns=sh.getRow(1).getLastCellNum();
for(int i=0;i<noofrows;i++)
          {
String r=sh.getRow(i).getCell(0).toString();
if(r.equals(row))
{
for(int j=0;j<noofcolumns;j++)
         {
String c=sh.getRow(0).getCell(j).toString();
if(c.equals(column)){
try{
double value=sh.getRow(i).getCell(j).getNumericCellValue();
        String data=BigDecimal.valueOf(value).toPlainString();
return data;
}catch(Exception e){
        String data1=sh.getRow(i).getCell(j).getStringCellValue();
return data1;
}}}}}
return column;
}catch(Exception e){}
return column;

}

Launch IOS Application using Appium(Selenium)


Code for Launching IOS application::

Below code will help u to launch IPA in Real Device.

Find another blog for setting up Xcode operations.


public AppiumDriver Launch_IOSApp()
{
File file = new File(s);
//Launching IOS App
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("platformVersion", "9.3");
capabilities.setCapability("deviceOrientation", "portrait");
capabilities.setCapability("browserName", "");
capabilities.setCapability("automationName", "XCUITest");
capabilities.setCapability("deviceName", "iphone");
capabilities.setCapability("udid", "fabad62540cc0088bfc9f2af5c154985420a856e");
capabilities.setCapability("xcodeConfigFile", "/usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent/Config.xcconfig");
capabilities.setCapability("appPath",file.getAbsolutePath());
capabilities.setCapability("realDeviceLogger","/usr/local/lib/node_modules/deviceconsole");
capabilities.setCapability("bundleId","com.proj.ios");
try {
idriver = new IOSDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return idriver;

}

Enable & Disable WIFI in Android

Enabling WIFI ::



public void enablewifi() throws InterruptedException{
NetworkConnectionSetting connection_wifi_on = new NetworkConnectionSetting(false, true, false);
Thread.sleep(10000);
Reporter.log("Enabled WIFI connection");

}




Disable WIFI ::

public  void disbalewifi() throws InterruptedException, MalformedURLException{
DesiredCapabilities capabilities = new DesiredCapabilities();
AndroidDriver<MobileElement> driver = new AndroidDriver<MobileElement>(new URL("http://localhost:4723/wd/hub"), capabilities);

// init wi-fi and data enabled and airplane mode disabled
NetworkConnectionSetting ncs = new NetworkConnectionSetting(false, true, true);
driver.setNetworkConnection(ncs);
// turn off wi-fi
ncs.setWifi(false);
driver.setNetworkConnection(ncs);
NetworkConnectionSetting conn = new NetworkConnectionSetting(false, false, false);
conn.setWifi(false);
Thread.sleep(10000);
Reporter.log("Disabled WIFI connection");
}

Appium Start & Stop via CMD(Using SeleniumJava)

Start Appium using CMD::


public void startAppium() throws IOException, InterruptedException { 
Reporter.log("Starting Appium Server ", true);
String AppiumNodeFilePath = getPropertyOf("AppiumNodeFilePath");
        String AppiumJavaScriptServerFile = getPropertyOf("AppiumJavaScriptServerFile");
        String AppiumServerAddress = getPropertyOf("AppiumServerAddress");
        String AppiumServerPort = getPropertyOf("AppiumServerPort");
CommandLine command = new CommandLine("cmd"); 
command.addArgument("/c"); 
command.addArgument(AppiumNodeFilePath); 
command.addArgument(AppiumJavaScriptServerFile); 
command.addArgument("--address"); 
command.addArgument(AppiumServerAddress); 
command.addArgument("--port"); 
command.addArgument(AppiumServerPort); 
command.addArgument("--no-reset");
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); 
DefaultExecutor executor = new DefaultExecutor(); 
executor.setExitValue(1); 
executor.execute(command, resultHandler); 
AppiumServiceBuilder builder = new AppiumServiceBuilder();
builder.withArgument(GeneralServerFlag.LOG_LEVEL, "info");
// Wait for 15 minutes so that appium server can start properly before going for test execution. // Increase this time If face any error. 
Thread.sleep(12000);

}



Stop Appium Server ::



public void stopAppium() throws IOException, InterruptedException { 
// Add different arguments In command line which requires to stop appium server. 
Reporter.log("Stopping Appium server", true);
Thread.sleep(2000);
CommandLine command = new CommandLine("cmd"); 
command.addArgument("/c"); 
command.addArgument("taskkill"); 
command.addArgument("/F"); 
command.addArgument("/IM"); 
command.addArgument("node.exe"); 
// Execute command line arguments to stop appium server. 
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); 
DefaultExecutor executor = new DefaultExecutor(); 
executor.setExitValue(1); 
executor.execute(command, resultHandler);
}

Parallel Execution With Different Users .. using TestNG & DataProviders

Concurrent Execution with Different Browsers & Different Users.


BROWSER::

Set UP Browser to be Launched.

public WebDriver LaunchBrowser()
{
String browser = getpropvalue("Browser");
if (browser.equalsIgnoreCase("ff")) 
{
wdriver = new FirefoxDriver();
}
else if (browser.equalsIgnoreCase("chrome")) 
{
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver",
getpropvalue("chromepath"));
wdriver = new ChromeDriver(options);
wdriver.manage().window().maximize();
}
else if (browser.equalsIgnoreCase("ie")) 
{
System.setProperty("webdriver.ie.driver", "./drivers/IEDriverServer_x32_v2.47.0.exe");
wdriver = new InternetExplorerDriver();
}
else if (browser.equalsIgnoreCase("safari")) 
{
wdriver = new SafariDriver();
}
else if (browser.equalsIgnoreCase("opera")) 
{
wdriver = new OperaDriver();
}
launchBrowser();
return wdriver;
} 


Naviagte URL::

public void navigateurl()
{
              wdriver.get("URL");
}

Login Setup::

public void enterCredentials_For_multiple(String email)
{

             wdriver.findElement(By.xpath("//Login")).click();
      wdriver.findElement(By.xpath("")).sendKeys(email);
wdriver.findElement(By.xpath("Password")).sendKeys("");

}

Data Providers ::

This will help to provide different set of Data.

1.First Approach

@DataProvider(parallel = true)
public Object[][] getdata() throws EncryptedDocumentException, InvalidFormatException, IOException
{
Object[][] data = {{"harikrishna@gmail.com"},{"test@gmail.com"},{"test2@gmail.com"}};
return data;
}

2.Second Approach ::

Read From Excel File with Passing Different Set of Data

public String getLoginData(String uName) throws IOException, InvalidFormatException
{
String pass = null;
FileInputStream fis=new FileInputStream("../testdata/securityTestData.xls");
Workbook wb=WorkbookFactory.create(fis);
Sheet sh=wb.getSheet("concurrentLogin");
int noOfRows=sh.getLastRowNum();
for(int i=1;i<=noOfRows;i++)
{
Row row=sh.getRow(i);
if(row.getCell(0).getStringCellValue()==uName)
pass=row.getCell(1).getStringCellValue();
}
return pass;
}



Now Set testNg.xml ::