Skip to content

Instantly share code, notes, and snippets.

@santocodez
santocodez / SetUpAppium.java
Last active November 22, 2016 10:31
Set up appium - create android driver instance
AndroidDriver<MobileElement> driver;
@BeforeClass
public void setup() throws IOException, InterruptedException{
//startAppiumServer();
File appDir = new File("src");
File app = new File(appDir,"app.apk");
DesiredCapabilities cap =new DesiredCapabilities();
cap.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, "5.1");
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Moto E");
@santocodez
santocodez / SwitchTabsSelenium.java
Created November 22, 2016 07:33
Switch Tabs in Selenium
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
String actUrl = driver.getCurrentUrl();
driver.close();
driver.switchTo().window(tabs.get(0));
Thread.sleep(2000);
@santocodez
santocodez / MobileWebSetUpSelenium.java
Last active September 1, 2017 20:07
Mobile Web Set Up in Selenium before class
public static WebDriver driver;
@BeforeClass
public void setup() throws InterruptedException{
System.setProperty("webdriver.chrome.driver", "C:\\QASetup\\chromedriver.exe");
HashMap<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "Google Nexus 5");
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("mobileEmulation", mobileEmulation);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
@santocodez
santocodez / HideKeyBoardAppium.java
Last active November 22, 2016 10:14
Hide Key Board Appium
public static void hideKeyBoard(AndroidDriver<MobileElement> driver){
driver.getKeyboard();
driver.hideKeyboard();
driver.pressKeyCode(AndroidKeyCode.BACK);
}
@santocodez
santocodez / SetDateAndroidFour.java
Last active November 22, 2016 10:31
Set Date Android < 4 default date picker
public static void setDate2(AndroidDriver<MobileElement> driver,String month,int date){
month = month.substring(0, 3);
List<MobileElement> ele = driver.findElements(By.id("android:id/numberpicker_input"));
ele.get(0).click();
List<MobileElement> eles = driver.findElements(By.className("android.widget.Button"));
System.out.println(eles.size());
while(! (ele.get(0).getAttribute("name").equals(month))){
eles.get(1).click();
}
ele.get(1).click();
@santocodez
santocodez / SetDateAndroidFive.java
Last active November 22, 2016 10:32
Set Date for Android phones > 5.0 Default date picker
public static final By DATE_FIELD = By.id("com.confirmtkt.lite:id/datelinearlayout");
public static final By DATE_PICKER_2 = By.className("android.view.View");
public static final By DATE_CANCEL = By.id("android:id/button2");
public static final By DATE_PICKER = By.id("android:id/date_picker_day");
public static final By MONTH_PICKER = By.id("android:id/date_picker_month");
public static final By YEAR_PICKER = By.id("android:id/date_picker_year");
public static final By DATE_OK = By.id("android:id/button1");
public static void setDate(AndroidDriver<MobileElement> driver,int reqDateNum, String reqMonth, int reqYear) throws ParseException{
boolean isDateSet = false;
@santocodez
santocodez / TakeScreenshotAfterMethod.java
Last active November 22, 2016 10:17
Implementing take screenshot after every method
@AfterMethod
public void onTestFailure(ITestResult result) {
String testName = result.getMethod().getMethodName().toUpperCase();
if (ITestResult.FAILURE == result.getStatus())
takeScreenShot(driver, "Sanity\\Failed\\"+testName+"","Project_name");
else if (ITestResult.SUCCESS == result.getStatus())
takeScreenShot(driver, "Sanity\\Passed\\"+testName+"","Alternates");
else
takeScreenShot(driver, "Sanity\\Skipped\\"+testName+"","Alternates");
}
@santocodez
santocodez / GetDates.java
Last active November 22, 2016 10:18
Get Today or next day date
public String getTodayDate(String format){
DateFormat df = new SimpleDateFormat(format);
Calendar calobj = Calendar.getInstance();
String CurrentDate = df.format(calobj.getTime());
return CurrentDate;
}
public String getNextDays(String format,int noDays) throws ParseException{
DateFormat df = new SimpleDateFormat(format);
Calendar calobj = Calendar.getInstance();
String CurrentDate = df.format(calobj.getTime());
@santocodez
santocodez / WebDriverSetUp.java
Last active November 22, 2016 10:18
Web driver setup before class
WebDriver driver;
@BeforeClass
public void setup() throws ParseException{
System.setProperty("webdriver.chrome.driver", "C:\\QASetup\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-extensions");
options.addArguments("--no-sandbox");
driver = new ChromeDriver(options);
driver.manage().window().maximize();
}
@santocodez
santocodez / GetIntvalueFromString.java
Last active November 22, 2016 10:30
Get Int value From String
public static int getIntvalue(String text){
int value = Integer.parseInt(text.replaceAll("\\D+",""));
return value;
}