├── .classpath ├── .gitignore ├── .project ├── .settings ├── org.eclipse.core.resources.prefs ├── org.eclipse.jdt.core.prefs └── org.eclipse.m2e.core.prefs ├── LICENSE ├── README.md ├── Screenshots └── gmail_login_i_login_with_valid_credential.png ├── pom.xml ├── selenium-cucumber-java-0.0.1.pom └── src ├── main └── java │ ├── appUnderTest │ └── AndroidCalculator.apk │ ├── browserConfigs │ ├── browserstack_android7_chrome.properties │ ├── browserstack_win10_chrome.properties │ ├── local_android_app_nexus5.properties │ ├── local_android_nexus5.properties │ ├── saucelab_android6_chrome.properties │ ├── saucelab_android_app.properties │ ├── saucelab_android_app_nexus.properties │ ├── saucelab_android_chrome.properties │ └── saucelab_windows_chrome52.properties │ └── platformConfigs │ ├── browserstack.properties │ └── saucelab.properties └── test ├── java ├── env │ ├── DriverUtil.java │ ├── Hooks.java │ └── RunCukeTest.java └── info │ └── seleniumcucumber │ └── userStepDefintions │ └── UserStepDefinitions.java └── resources └── features ├── calculator.feature └── my_first.feature /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /Screenshots 3 | .classpath 4 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | sc-java-testing 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/test/java=UTF-8 4 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 12 | org.eclipse.jdt.core.compiler.source=1.8 13 | -------------------------------------------------------------------------------- /.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 selenium-cucumber 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | selenium-cucumber-java-maven 2 | ================= 3 | 4 | selenium-cucumber : Automation Testing Using Java 5 | 6 | selenium-cucumber is a behavior driven development (BDD) approach to write automation test script to test Web. 7 | It enables you to write and execute automated acceptance/unit tests. 8 | It is cross-platform, open source and free. 9 | Automate your test cases with minimal coding. 10 | [More Details](http://seleniumcucumber.info/) 11 | 12 | Documentation 13 | ------------- 14 | * [Installation](https://github.com/selenium-cucumber/selenium-cucumber-java/blob/master/doc/installation.md) 15 | * [Predefined steps](https://github.com/selenium-cucumber/selenium-cucumber-java/blob/master/doc/canned_steps.md) 16 | 17 | Download a Framework 18 | -------------- 19 | * Maven - https://github.com/selenium-cucumber/selenium-cucumber-java-maven-example 20 | 21 | Framework Architecture 22 | -------------- 23 | Project-Name 24 | | 25 | |_src/main/java 26 | | |_appUnderTest 27 | | | |_calc.apk 28 | | | |... 29 | | |_browserConfigs 30 | | | |_saucelab_windows_chrome.properties 31 | | | |_browserstack_windows_chrome.properties 32 | | | |... 33 | | |_platformConfigs 34 | | |_saucelab.properties 35 | | |_browserstack.properties 36 | | |... 37 | |_src/main/resources 38 | |_src/test/java 39 | | |_env 40 | | | |_DriverUtil.java 41 | | | |_Hooks.java 42 | | | |_RunCukeTest.java 43 | | |_userStepDefinitions 44 | | | |_loginSteps.java 45 | | | |_signUpSteps.java 46 | | | |... 47 | |_src/test/resources 48 | | |_features 49 | | | |_login.feature 50 | | | |_signUp.feature 51 | 52 | * **src/test/resources/features** - all the cucumber features files (files .feature ext) goes here. 53 | * **src/test/java/userStepDefinition** - you can define step defintion under this package for your feature steps. 54 | * **src/test/java/env** - this package contains cucumber runner (RunCukeTest.java) where you can configure your glue code location (step defintions), define test result output format.(html, json, xml). Hooks where you can configure all before and after test settings Hooks.java, DriverUtil.java contains code for intializing driver instances for respective driver. 55 | * **src/main/java/platformConfigs** - If you want to run your test on saucelab and browserstack platforms, you need to add its configuration such as username, access key here. 56 | * **src/main/java/browserConfig** - When you run your test on remote browser/platform you have to provide capabilities and platform information here. 57 | * **src/main/java/appUnderTest** - If you are testing mobile based application you can keep your app build here. 58 | 59 | Writing a test 60 | -------------- 61 | 62 | The cucumber features goes in the `features` library and should have the ".feature" extension. 63 | 64 | You can start out by looking at `features/my_first.feature`. You can extend this feature or make your own features using some of the [predefined steps](doc/canned_steps.md) that comes with selenium-cucumber. 65 | 66 | 67 | Predefined steps 68 | ----------------- 69 | By using predefined steps you can automate your test cases more quickly, more efficiently and without much coding. 70 | 71 | The predefined steps are located [here](doc/canned_steps.md) 72 | 73 | Running test 74 | -------------- 75 | 76 | Go to your project directory from terminal and hit following commands 77 | * `mvn test (defualt will run on local firefox browser)` 78 | * `mvn test "-Dbrowser=chrome" (to use any other browser)` 79 | * `mvn test -Dcucumber.options="classpath:features/my_first.feature"` to run specific feature. 80 | * `mvn test -Dcucumber.options="–-plugin html:target/result-html"` to generate a HTML report. 81 | * `mvn test -Dcucumber.options="–-plugin json:target/result-json"` to generate a JSON report. 82 | 83 | Running test On remote browser/platform 84 | --------------------------------------- 85 | 86 | To run test on saucelab, browserstack or any other remote browser you need to create browser config file under src/main/java/browserConfig 87 | 88 | To run on saucelab create config file with name preceding with saucelab 89 | - saucelab_windows_chrome.properties 90 | - saucelab_mac_firefox.properties 91 | 92 | * `mvn test "-Dconfig=saucelab_mac_firefox"` 93 | 94 | To run on browserstack create config file with name preceding with browserstack 95 | - browserstack_windows_chrome.properties 96 | - browserstack_mac_firefox.properties 97 | 98 | * `mvn test "-Dconfig=browserstack_mac_firefox"` 99 | 100 | Maven/Gradle Dependency 101 | ----------------------- 102 | 103 | See https://jitpack.io/#selenium-cucumber/selenium-cucumber-java . 104 | 105 | License 106 | ------- 107 | 108 | (The MIT License) 109 | 110 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 111 | 112 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 113 | 114 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 115 | -------------------------------------------------------------------------------- /Screenshots/gmail_login_i_login_with_valid_credential.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/selenium-cucumber/selenium-cucumber-java-maven-example/98d5e7b4cadb6acbba8ba42266beffd3b5efc16d/Screenshots/gmail_login_i_login_with_valid_credential.png -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | sc-java-testing 4 | sc-java-testing 5 | 0.0.1-SNAPSHOT 6 | 7 | 8 | 9 | jitpack.io 10 | https://jitpack.io 11 | 12 | 13 | 14 | 15 | 16 | 17 | info.cukes 18 | cucumber-java 19 | 1.2.5 20 | 21 | 22 | 23 | 24 | info.cukes 25 | cucumber-junit 26 | 1.2.5 27 | 28 | 29 | 30 | 31 | junit 32 | junit 33 | 4.12 34 | 35 | 36 | 37 | 38 | org.seleniumhq.selenium 39 | selenium-java 40 | 3.5.2 41 | 42 | 43 | 44 | org.seleniumhq.selenium 45 | selenium-server 46 | 3.5.2 47 | 48 | 49 | com.github.selenium-cucumber 50 | selenium-cucumber-java 51 | 1.0.1-beta 52 | 53 | 54 | 55 | 56 | io.appium 57 | java-client 58 | 5.0.4 59 | 60 | 61 | 62 | 63 | 64 | 65 | org.apache.maven.plugins 66 | maven-compiler-plugin 67 | 3.2 68 | 69 | UTF-8 70 | 1.8 71 | 1.8 72 | -Werror 73 | 74 | 75 | 76 | browser 77 | ${browser} 78 | 79 | 80 | cloud_config 81 | ${cloud_config} 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /selenium-cucumber-java-0.0.1.pom: -------------------------------------------------------------------------------- 1 | pom.xml -------------------------------------------------------------------------------- /src/main/java/appUnderTest/AndroidCalculator.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/selenium-cucumber/selenium-cucumber-java-maven-example/98d5e7b4cadb6acbba8ba42266beffd3b5efc16d/src/main/java/appUnderTest/AndroidCalculator.apk -------------------------------------------------------------------------------- /src/main/java/browserConfigs/browserstack_android7_chrome.properties: -------------------------------------------------------------------------------- 1 | name=GooglePixelTest 2 | device=Google Pixel 3 | realMobile=true 4 | os_version=7.1 5 | browser=Android -------------------------------------------------------------------------------- /src/main/java/browserConfigs/browserstack_win10_chrome.properties: -------------------------------------------------------------------------------- 1 | name=Windows10ChromeTest 2 | browser=Chrome 3 | browser_version=62.0 4 | os=Windows 5 | os_version=10 6 | resolution=1024x768 -------------------------------------------------------------------------------- /src/main/java/browserConfigs/local_android_app_nexus5.properties: -------------------------------------------------------------------------------- 1 | deviceName=Nexus5 2 | platformName=android 3 | platformVersion=7.0 4 | app=AndroidCalculator.apk 5 | udid=192.168.49.101:5555 -------------------------------------------------------------------------------- /src/main/java/browserConfigs/local_android_nexus5.properties: -------------------------------------------------------------------------------- 1 | deviceName=Nexus5 2 | platformName=android 3 | platformVersion=7.0 4 | browserName=Chrome 5 | udid=2454f24e -------------------------------------------------------------------------------- /src/main/java/browserConfigs/saucelab_android6_chrome.properties: -------------------------------------------------------------------------------- 1 | name=Android-Test 2 | appiumVersion=1.6.4 3 | deviceName=Android Emulator 4 | deviceOrientation=portrait 5 | browserName=Chrome 6 | platformVersion=6.0 7 | platformName=Android -------------------------------------------------------------------------------- /src/main/java/browserConfigs/saucelab_android_app.properties: -------------------------------------------------------------------------------- 1 | name=CalcTest 2 | appiumVersion=1.6.4 3 | deviceName=Android GoogleAPI Emulator 4 | deviceOrientation=portrait 5 | platformVersion=7.0 6 | platformName=Android 7 | app=sauce-storage:AndroidCalculator.apk -------------------------------------------------------------------------------- /src/main/java/browserConfigs/saucelab_android_app_nexus.properties: -------------------------------------------------------------------------------- 1 | name=Android-Test 2 | appiumVersion=1.6.4 3 | deviceName=Android Emulator 4 | deviceOrientation=portrait 5 | app=sauce-storage:AndroidCalculator.apk 6 | platformVersion=6.0 7 | platformName=Android -------------------------------------------------------------------------------- /src/main/java/browserConfigs/saucelab_android_chrome.properties: -------------------------------------------------------------------------------- 1 | name=CalcTest 2 | appiumVersion=1.6.4 3 | deviceName=Android GoogleAPI Emulator 4 | deviceOrientation=portrait 5 | platformVersion=7.0 6 | platformName=Android 7 | browserName=chrome -------------------------------------------------------------------------------- /src/main/java/browserConfigs/saucelab_windows_chrome52.properties: -------------------------------------------------------------------------------- 1 | name=Win10-Chrome49 2 | platform=Windows 10 3 | browserName=Chrome 4 | version=49.0 5 | -------------------------------------------------------------------------------- /src/main/java/platformConfigs/browserstack.properties: -------------------------------------------------------------------------------- 1 | username=your_username 2 | access_key=your_access_key 3 | url=@hub-cloud.browserstack.com/wd/hub 4 | protocol=http -------------------------------------------------------------------------------- /src/main/java/platformConfigs/saucelab.properties: -------------------------------------------------------------------------------- 1 | username=your_username 2 | access_key=your_access_key 3 | url=@ondemand.saucelabs.com:80/wd/hub 4 | protocol=http -------------------------------------------------------------------------------- /src/test/java/env/DriverUtil.java: -------------------------------------------------------------------------------- 1 | package env; 2 | 3 | import java.io.BufferedInputStream; 4 | import java.io.BufferedOutputStream; 5 | import java.io.File; 6 | import java.io.FileInputStream; 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import java.net.HttpURLConnection; 10 | import java.net.MalformedURLException; 11 | import java.net.URL; 12 | import java.net.URLConnection; 13 | import java.util.Base64; 14 | import java.util.Enumeration; 15 | import java.util.Properties; 16 | import java.util.concurrent.TimeUnit; 17 | 18 | import org.openqa.selenium.By; 19 | import org.openqa.selenium.NoSuchSessionException; 20 | import org.openqa.selenium.SessionNotCreatedException; 21 | import org.openqa.selenium.WebDriver; 22 | import org.openqa.selenium.WebElement; 23 | import org.openqa.selenium.chrome.ChromeDriver; 24 | import org.openqa.selenium.chrome.ChromeOptions; 25 | import org.openqa.selenium.edge.EdgeDriver; 26 | import org.openqa.selenium.firefox.FirefoxDriver; 27 | import org.openqa.selenium.firefox.FirefoxOptions; 28 | import org.openqa.selenium.remote.DesiredCapabilities; 29 | import org.openqa.selenium.remote.ErrorHandler; 30 | import org.openqa.selenium.remote.RemoteWebDriver; 31 | import org.openqa.selenium.safari.SafariDriver; 32 | import org.openqa.selenium.support.ui.ExpectedConditions; 33 | import org.openqa.selenium.support.ui.WebDriverWait; 34 | 35 | import io.appium.java_client.android.AndroidDriver; 36 | import io.appium.java_client.ios.IOSDriver; 37 | 38 | public class DriverUtil { 39 | public static long DEFAULT_WAIT = 20; 40 | protected static WebDriver driver=null; 41 | static String currentPath = System.getProperty("user.dir"); 42 | static Properties prop = new Properties(); 43 | static DesiredCapabilities capability=null; 44 | 45 | public static DesiredCapabilities getCapability(InputStream input) { 46 | DesiredCapabilities capability = new DesiredCapabilities(); 47 | try { 48 | prop.load(input); 49 | if(prop.containsKey("app")) { 50 | String appName = prop.getProperty("app"); 51 | if(!appName.contains("sauce-storage")) { 52 | String appPath = currentPath+"/src/main/java/appUnderTest/"+appName; 53 | prop.setProperty("app", appPath); 54 | } 55 | } 56 | 57 | // set capabilities 58 | Enumeration enuKeys = prop.keys(); 59 | while (enuKeys.hasMoreElements()) { 60 | String key = (String) enuKeys.nextElement(); 61 | String value = prop.getProperty(key); 62 | capability.setCapability(key, value); 63 | } 64 | input.close(); 65 | }catch(Exception e) { 66 | e.printStackTrace(); 67 | System.exit(0); 68 | } 69 | return capability; 70 | } 71 | 72 | private static void uploadAppToSauceStorage(String appName, String appPath) { 73 | System.out.println("Uploading App "+appName+" to sauce storage"); 74 | InputStream input; 75 | try { 76 | input = new FileInputStream(currentPath+"/src/main/java/platformConfigs/saucelab.properties"); 77 | prop.load(input); 78 | } catch (IOException e1) { 79 | e1.printStackTrace(); 80 | } 81 | 82 | String username = prop.getProperty("username"); 83 | String access_key = prop.getProperty("access_key"); 84 | 85 | String uploadURL = "https://saucelabs.com/rest/v1/storage/"+username+"/"+appName+"?overwrite=true"; 86 | String encoding = Base64.getEncoder().encodeToString((username+":"+access_key).getBytes()); 87 | 88 | URLConnection urlconnection = null; 89 | try { 90 | File file = new File(appPath); 91 | URL url = new URL(uploadURL); 92 | urlconnection = url.openConnection(); 93 | urlconnection.setDoOutput(true); 94 | urlconnection.setDoInput(true); 95 | 96 | if (urlconnection instanceof HttpURLConnection) { 97 | ((HttpURLConnection) urlconnection).setRequestMethod("POST"); 98 | ((HttpURLConnection) urlconnection).setRequestProperty("Content-type", "text/plain"); 99 | ((HttpURLConnection) urlconnection).setRequestProperty("Authorization", "Basic " + encoding); 100 | ((HttpURLConnection) urlconnection).connect(); 101 | } 102 | 103 | BufferedOutputStream bos = new BufferedOutputStream(urlconnection.getOutputStream()); 104 | BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); 105 | int i; 106 | // read byte by byte until end of stream 107 | while ((i = bis.read()) > 0) { 108 | bos.write(i); 109 | } 110 | bis.close(); 111 | bos.close(); 112 | } catch (Exception e) { 113 | e.printStackTrace(); 114 | } 115 | try { 116 | 117 | int responseCode = ((HttpURLConnection) urlconnection).getResponseCode(); 118 | if ((responseCode >= 200) && (responseCode <= 202)) { 119 | System.out.println("App uploaded successfully"); 120 | } 121 | else { 122 | System.out.println("App upload failed"); 123 | } 124 | System.out.println("responseCode : "+responseCode); 125 | 126 | ((HttpURLConnection) urlconnection).disconnect(); 127 | 128 | } catch (IOException e) { 129 | e.printStackTrace(); 130 | } 131 | } 132 | 133 | public static WebDriver getDefaultDriver() { 134 | if (driver != null) { 135 | return driver; 136 | } 137 | 138 | String enviroment = "desktop"; 139 | String platform = ""; 140 | String config = System.getProperty("config", ""); 141 | 142 | if(!config.isEmpty()) 143 | { 144 | try{ 145 | enviroment = config.split("_")[0].toLowerCase(); 146 | platform = config.split("_")[1].toLowerCase(); 147 | InputStream input = new FileInputStream(currentPath+"/src/main/java/browserConfigs/"+config+".properties"); 148 | capability = getCapability(input); 149 | } 150 | catch(Exception e){ 151 | System.out.println("\nException : File not present or Invalid config file name "+config+".properties"); 152 | System.out.println("Config file format should be : enviroment_platform_device.properties"); 153 | System.out.println("\nE.g : local_android_nexus5.properties"); 154 | System.out.println("E.g : local_ios_iphone6.properties"); 155 | System.out.println("E.g : browserstack_android_nexus5.properties"); 156 | System.out.println("E.g : saucelab_windows7_chrome.properties"); 157 | System.exit(0); 158 | } 159 | } 160 | 161 | switch(enviroment) 162 | { 163 | case "local": if(platform.equals("android")) 164 | driver = androidDriver(capability); 165 | else if(platform.equals("ios")) 166 | driver = iosDriver(capability); 167 | else{ 168 | System.out.println("unsupported platform"); 169 | System.exit(0); 170 | } 171 | break; 172 | 173 | case "browserstack": driver = browserStackDriver(capability); 174 | break; 175 | 176 | case "saucelab": driver = saucelabDriver(capability); 177 | break; 178 | 179 | case "desktop": DesiredCapabilities capabilities = null; 180 | capabilities = DesiredCapabilities.firefox(); 181 | capabilities.setJavascriptEnabled(true); 182 | capabilities.setCapability("takesScreenshot", true); 183 | driver = chooseDriver(capabilities); 184 | driver.manage().timeouts().setScriptTimeout(DEFAULT_WAIT, TimeUnit.SECONDS); 185 | driver.manage().window().maximize(); 186 | break; 187 | 188 | default : System.out.println("\nException : Invalid platform "+enviroment); 189 | System.exit(0); 190 | } 191 | 192 | return driver; 193 | } 194 | 195 | /* 196 | * Returns saucelab remote driver instance by reading saucelab configuration 197 | * from platformConfigs/saucelab.properties 198 | * 199 | * @param DesiredCapabilities create capabilities by reading browser config. 200 | * @return RemoteWebDriver 201 | */ 202 | private static WebDriver saucelabDriver(DesiredCapabilities capabilities) { 203 | URL remoteDriverURL = null; 204 | RemoteWebDriver remoteDriver = null; 205 | 206 | // set app path for app testing 207 | if(prop.containsKey("app")) { 208 | String appName = prop.getProperty("app").split(":")[1]; 209 | String appPath = currentPath+"/src/main/java/appUnderTest/"+appName; 210 | 211 | File appFile = new File(appPath); 212 | if(appFile.exists()) { 213 | //prop.setProperty("app", appPath); 214 | uploadAppToSauceStorage(appName, appPath); 215 | }else { 216 | System.out.println("Exception : No app with name '"+appName+"' found in appUnderTest directory"); 217 | System.exit(0); 218 | } 219 | } 220 | 221 | 222 | try { 223 | InputStream input = new FileInputStream(currentPath+"/src/main/java/platformConfigs/saucelab.properties"); 224 | prop.load(input); 225 | 226 | String url = prop.getProperty("protocol")+ 227 | "://"+ 228 | prop.getProperty("username")+ 229 | ":"+ 230 | prop.getProperty("access_key")+ 231 | prop.getProperty("url"); 232 | 233 | input.close(); 234 | prop.clear(); 235 | remoteDriverURL = new URL(url); 236 | remoteDriver = new RemoteWebDriver(remoteDriverURL, capability); 237 | }catch(Exception e) { 238 | System.out.println("\nException Occured :\n"); 239 | System.out.println(e.getMessage()); 240 | System.exit(0); 241 | } 242 | return remoteDriver; 243 | } 244 | 245 | /* 246 | * Returns browserStack remote driver instance by reading browserStack configuration 247 | * from platformConfigs/browserstack.properties 248 | * 249 | * @param DesiredCapabilities create capabilities by reading browser config. 250 | * @return RemoteWebDriver 251 | */ 252 | private static WebDriver browserStackDriver(DesiredCapabilities capabilities) { 253 | URL remoteDriverURL = null; 254 | try { 255 | InputStream input = new FileInputStream(currentPath+"/src/main/java/platformConfigs/browserstack.properties"); 256 | prop.load(input); 257 | 258 | String url = prop.getProperty("protocol")+ 259 | "://"+ 260 | prop.getProperty("username")+ 261 | ":"+ 262 | prop.getProperty("access_key")+ 263 | prop.getProperty("url"); 264 | 265 | input.close(); 266 | prop.clear(); 267 | remoteDriverURL = new URL(url); 268 | }catch(Exception e) { 269 | e.printStackTrace(); 270 | } 271 | return new RemoteWebDriver(remoteDriverURL, capability); 272 | } 273 | 274 | private static WebDriver androidDriver(DesiredCapabilities capabilities) { 275 | String port = "4723"; 276 | try { 277 | driver = (AndroidDriver) new AndroidDriver(new URL("http://127.0.0.1:"+port+"/wd/hub"),capabilities); 278 | } catch (MalformedURLException e) { 279 | e.printStackTrace(); 280 | } 281 | return driver; 282 | } 283 | 284 | private static WebDriver iosDriver(DesiredCapabilities capabilities) 285 | { 286 | String port = "4723"; 287 | try { 288 | driver = (IOSDriver) new IOSDriver(new URL("http://127.0.0.1:"+port+"/wd/hub"),capabilities); 289 | } catch (MalformedURLException e) { 290 | e.printStackTrace(); 291 | } 292 | return driver; 293 | } 294 | 295 | /** 296 | * By default to web driver will be firefox 297 | * 298 | * Override it by passing -Dbrowser=Chrome to the command line arguments 299 | * @param capabilities 300 | * @return webdriver 301 | */ 302 | private static WebDriver chooseDriver(DesiredCapabilities capabilities) { 303 | String preferredDriver = System.getProperty("browser", "Firefox"); 304 | boolean headless = System.getProperty("headless", "false").equals("true"); 305 | 306 | switch (preferredDriver.toLowerCase()) { 307 | case "safari": 308 | try { 309 | driver = new SafariDriver(); 310 | }catch(Exception e) { 311 | System.out.println(e.getMessage()); 312 | System.exit(0); 313 | } 314 | return driver; 315 | case "edge": 316 | try { 317 | driver = new EdgeDriver(); 318 | }catch(Exception e) { 319 | System.out.println(e.getMessage()); 320 | System.exit(0); 321 | } 322 | return driver; 323 | case "chrome": 324 | final ChromeOptions chromeOptions = new ChromeOptions(); 325 | if (headless) { 326 | chromeOptions.addArguments("--headless"); 327 | } 328 | capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions); 329 | try 330 | { 331 | driver = new ChromeDriver(capabilities); 332 | ErrorHandler handler = new ErrorHandler(); 333 | handler.setIncludeServerErrors(false); 334 | //driver.setErrorHandler(handler); 335 | }catch(Exception e) { 336 | System.out.println(e.getMessage()); 337 | System.exit(0); 338 | } 339 | return driver; 340 | default: 341 | FirefoxOptions options = new FirefoxOptions(); 342 | if (headless) { 343 | options.addArguments("-headless", "-safe-mode"); 344 | } 345 | capabilities.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options); 346 | try { 347 | driver = new FirefoxDriver(capabilities); 348 | } 349 | catch(Exception e) { 350 | System.out.println(e.getMessage()); 351 | System.exit(0); 352 | } 353 | return driver; 354 | } 355 | } 356 | 357 | public static WebElement waitAndGetElementByCssSelector(WebDriver driver, String selector, 358 | int seconds) { 359 | By selection = By.cssSelector(selector); 360 | return (new WebDriverWait(driver, seconds)).until( // ensure element is visible! 361 | ExpectedConditions.visibilityOfElementLocated(selection)); 362 | } 363 | 364 | public static void closeDriver() { 365 | if (driver != null) { 366 | try { 367 | //driver.close(); 368 | //driver.quit(); // fails in current geckodriver! TODO: Fixme 369 | } catch (NoSuchMethodError nsme) { // in case quit fails 370 | } catch (NoSuchSessionException nsse) { // in case close fails 371 | } catch (SessionNotCreatedException snce) {} // in case close fails 372 | driver = null; 373 | } 374 | } 375 | } 376 | -------------------------------------------------------------------------------- /src/test/java/env/Hooks.java: -------------------------------------------------------------------------------- 1 | package env; 2 | 3 | import cucumber.api.Scenario; 4 | import cucumber.api.java.After; 5 | 6 | public class Hooks { 7 | @After("@NegativeTest") 8 | public void beforeScenario(Scenario scenario) { 9 | // System.out.println("In hooks"); 10 | // System.out.println(scenario.getName()); 11 | // System.out.println(scenario.getStatus()); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/env/RunCukeTest.java: -------------------------------------------------------------------------------- 1 | package env; 2 | 3 | import org.junit.runner.RunWith; 4 | 5 | import cucumber.api.CucumberOptions; 6 | import cucumber.api.junit.Cucumber; 7 | 8 | @RunWith(Cucumber.class) 9 | @CucumberOptions( 10 | plugin = { 11 | "pretty", "html:target/cucumberHtmlReport", 12 | "html:target/cucumberHtmlReport", // for html result 13 | "pretty:target/cucumber-json-report.json" // for json result 14 | }, 15 | features = "classpath:features", 16 | glue = {"info.seleniumcucumber.stepdefinitions", // predefined step definitions package 17 | "info.seleniumcucumber.userStepDefintions" // user step definitions package 18 | } 19 | ) 20 | 21 | public class RunCukeTest { 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/info/seleniumcucumber/userStepDefintions/UserStepDefinitions.java: -------------------------------------------------------------------------------- 1 | package info.seleniumcucumber.userStepDefintions; 2 | import org.junit.Assert; 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.support.ui.ExpectedConditions; 6 | import org.openqa.selenium.support.ui.WebDriverWait; 7 | 8 | import cucumber.api.java.en.Given; 9 | import env.DriverUtil; 10 | import info.seleniumcucumber.methods.BaseTest; 11 | 12 | 13 | public class UserStepDefinitions implements BaseTest { 14 | 15 | protected WebDriver driver = DriverUtil.getDefaultDriver(); 16 | 17 | @Given("^I should get logged-in$") 18 | public void should_logged_in() throws Throwable { 19 | 20 | By selection = By.id("flash"); 21 | (new WebDriverWait(driver, 30)).until( 22 | ExpectedConditions.visibilityOfElementLocated(selection)); 23 | String msg = driver.findElement(By.id("flash")).getText(); 24 | if(!msg.isEmpty()) 25 | msg = msg.split("\n")[0].trim(); 26 | Assert.assertEquals("You logged into a secure area!", msg); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/test/resources/features/calculator.feature: -------------------------------------------------------------------------------- 1 | Feature: Verify Calculator functionalities 2 | 3 | Scenario: Click on OK button 4 | Then I click on element having id "com.android2.calculator3:id/cling_dismiss" 5 | 6 | Scenario: Addition 7 | Then I click on element having id "com.android2.calculator3:id/digit5" 8 | Then I click on element having id "com.android2.calculator3:id/plus" 9 | Then I click on element having id "com.android2.calculator3:id/digit9" 10 | When I click on element having id "com.android2.calculator3:id/equal" 11 | Then element having xpath "//android.widget.EditText[@index=0]" should have text as "14" -------------------------------------------------------------------------------- /src/test/resources/features/my_first.feature: -------------------------------------------------------------------------------- 1 | Feature: Login 2 | As a user I should able to login into my app 3 | 4 | Scenario: I login with valid credential 5 | Given I navigate to "http://the-internet.herokuapp.com/login" 6 | And I enter "tomsmith" into input field having id "username" 7 | And I enter "SuperSecretPassword!" into input field having id "password" 8 | When I click on element having class "radius" 9 | Then I should get logged-in 10 | 11 | Scenario: Close browser 12 | Then I close browser --------------------------------------------------------------------------------