├── .idea ├── .gitignore ├── compiler.xml ├── jarRepositories.xml ├── misc.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── driver └── chromedriver ├── pom.xml └── src └── test ├── java ├── StepDef │ └── SearchGoogle.java └── TestRunner │ └── TestRunner.java └── resources └── Features └── SearchGoogle.feature /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # automation-bdd-google 2 | 3 | Hi:wave: Perkenalkan Namaku [Wisnu Munawar](https://www.instagram.com/wisnumnw):slightly_smiling_face:, biasa dipanggil Wisnu, Kamu dapat menemukanku di [Linkedin.](https://www.linkedin.com/in/wisnuwm) 4 |

Kali ini saya akan membagikan tutorial Test Automation menggunakan Selenium menggunakan bahasa pemrograman Java + Cucumber.

5 |

Semoga bermanfaat.

6 |

Kamu dapat menonton videonya disini

7 | 8 | 9 | **Tools yang dibutuhkan** : 10 | - [Java 15](https://www.oracle.com/java/technologies/javase/jdk15-archive-downloads.html) 11 | - [IntelliJ IDEA](https://www.jetbrains.com/idea/download/) 12 | - IntelliJ IDEA Plugin : [Cucumber for Java](https://plugins.jetbrains.com/plugin/7212-cucumber-for-java) 13 | - IntelliJ IDEA Plugin : [Gherkin](https://plugins.jetbrains.com/plugin/9164-gherkin) 14 | - [Chromedriver](https://chromedriver.chromium.org/downloads) 15 | or [Geckodriver](https://github.com/mozilla/geckodriver/releases) 16 | - [Maven](https://maven.apache.org/download.cgi) 17 | 18 | **Berikut adalah langkah2 nya :** 19 | 20 | 1. Buat Project pada IDE, disini saya menggunakan IntelliJ (https://www.jetbrains.com/idea/download/) 21 | Screen Shot 2021-10-23 at 14 42 25 22 | Screen Shot 2021-10-23 at 14 42 46 23 | 24 | 2. Install Plugin ini "Cucumber for Java" dan "Gherkin" 25 | 26 | Screen Shot 2021-10-23 at 14 45 34 27 | 28 | 3. Buka pom.xml dan add dependency ini 29 | ```xml 30 | 31 | 34 | 4.0.0 35 | 36 | org.example 37 | automation-bdd-google 38 | 1.0-SNAPSHOT 39 | 40 | 41 | 15 42 | 15 43 | 44 | 45 | 46 | 47 | 48 | io.cucumber 49 | cucumber-java 50 | 6.9.0 51 | 52 | 53 | 54 | 55 | junit 56 | junit 57 | 4.13.1 58 | test 59 | 60 | 61 | 62 | 63 | io.cucumber 64 | cucumber-junit 65 | 6.9.0 66 | test 67 | 68 | 69 | 70 | 71 | org.seleniumhq.selenium 72 | selenium-java 73 | 3.141.59 74 | 75 | 76 | 77 | 78 | net.masterthought 79 | cucumber-reporting 80 | 5.4.0 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | org.apache.maven.plugins 90 | maven-surefire-plugin 91 | 2.22.0 92 | 93 | true 94 | 95 | 96 | 97 | net.masterthought 98 | maven-cucumber-reporting 99 | 2.8.0 100 | 101 | 102 | execution 103 | verify 104 | 105 | generate 106 | 107 | 108 | automation-bdd-google 109 | ${project.build.directory}/cucumber-report-html 110 | ${project.build.directory}/cucumber.json 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | ``` 119 | 4. Tambahkan Webdriver (disini saya menggunakan ChromeDriver dan bisa di download di "https://chromedriver.chromium.org/downloads") lalu buat Directory driver dan simpan ChromeDriver 120 | Screen Shot 2021-10-23 at 14 50 28 121 | 122 | 5. Buat Directory "StepDef" dan "TestRunner" pada src/test/java dan juga buat Directory "Features" pada src/test/resources 123 | Screen Shot 2021-10-23 at 14 46 51 124 | 125 | 6. Buat Feature file "SearchGoogle.feature" pada directory Features 126 | ```gherkin 127 | Feature: Search Google 128 | Scenario: I want to using feature search on google 129 | Given I Open browser 130 | And Open website Google 131 | And Located on google website 132 | When I search "Wisnu Munawar" 133 | Then Showing result related with "Wisnu Munawar" 134 | ``` 135 | 136 | 7. Hover ke Gherkin scenarionya dan akan muncul action dan klik "More Action" lalu klik lagi "Create all step definitions" lalu buat nama class nya sesuai dengan nama Feature file dan klik "OK" 137 | Screen Shot 2021-10-23 at 14 52 34 138 | Screen Shot 2021-10-23 at 14 52 57 139 | Screen Shot 2021-10-23 at 14 53 28 140 | 141 | 8. Lalu akan otomatis ke generate scenarionya seperti dibawah ini 142 | Screen Shot 2021-10-23 at 14 54 47 143 | 144 | 9. Isi scenario di java code nya seperti dibawah ini ya 145 | ```java 146 | import io.cucumber.java.en.And; 147 | import io.cucumber.java.en.Given; 148 | import io.cucumber.java.en.Then; 149 | import io.cucumber.java.en.When; 150 | import org.openqa.selenium.By; 151 | import org.openqa.selenium.Keys; 152 | import org.openqa.selenium.WebDriver; 153 | import org.openqa.selenium.chrome.ChromeDriver; 154 | import org.openqa.selenium.chrome.ChromeOptions; 155 | 156 | public class SearchGoogle { 157 | WebDriver driver; 158 | @Given("I Open browser") 159 | public void iOpenBrowser() { 160 | final String dir = System.getProperty("user.dir"); 161 | System.out.println("current dir = " + dir); 162 | System.setProperty("webdriver.chrome.driver", dir+"/driver/chromedriver"); 163 | driver = new ChromeDriver(); 164 | } 165 | 166 | @And("Open website Google") 167 | public void openWebsiteGoogle() throws InterruptedException { 168 | driver.get("https://www.google.co.id/"); 169 | Thread.sleep(1000); 170 | } 171 | 172 | @And("Located on google website") 173 | public void locatedOnGoogleWebsite() { 174 | driver.findElement(By.name("btnK")).isDisplayed(); 175 | } 176 | 177 | @When("I search {string}") 178 | public void iSearch(String searchValue) { 179 | driver.findElement(By.name("q")).sendKeys(searchValue); 180 | driver.findElement(By.name("q")).sendKeys(Keys.ENTER); 181 | } 182 | 183 | @Then("Showing result related with {string}") 184 | public void showingResultRelatedWith(String result) { 185 | driver.findElement(By.xpath("//a[@href='https://id.linkedin.com/in/wisnuwm']")).isDisplayed(); 186 | String urlLinkedinWisnu = driver.findElement(By.xpath("//a[@href='https://id.linkedin.com/in/wisnuwm']")).getText(); 187 | System.out.println(urlLinkedinWisnu); 188 | driver.close(); 189 | driver.quit(); 190 | } 191 | } 192 | ``` 193 | Dan untuk cara mencari Elementnya bagaimana? 194 | Klik kanan aja lalu klik inspect element dan arahkan pada elementnya, contoh seperti dibawah ini : 195 | Screen Shot 2021-10-23 at 15 00 10 196 | Screen Shot 2021-10-23 at 15 01 17 197 | 198 | 199 | 10. Hover pada feature file dan klik Run 200 | Screen Shot 2021-10-23 at 15 28 17 201 | 202 | 11. Maka akan otomatis running test nya, seperti video dibawah ini : 203 | 204 | 205 | https://user-images.githubusercontent.com/54229493/138549271-4ad67366-9748-4df9-be78-0362eb72787d.mov 206 | 207 | 12. Buat java class "TestRunner" pada Directory TestRunner 208 | Screen Shot 2021-10-23 at 15 34 24 209 | 210 | 13. Lalu copy code ini 211 | ```java 212 | @RunWith(Cucumber.class) 213 | @CucumberOptions(features="src/test/resources/Features", 214 | glue= {"StepDef"}, 215 | plugin ={"pretty","json:target/cucumber.json"}) 216 | ``` 217 | Screen Shot 2021-10-23 at 15 35 10 218 | 219 | 14. Lalu open terminal pada IntelliJ dan ketikkan ```mvn test``` maka akan running test automation dan akan memunculkan result seperti ini 220 | Screen Shot 2021-10-23 at 15 38 32 221 | 222 | 15. Lalu ketikkan ```mvn verify -DskipTests``` untuk generate report test yang sudah kita jalankan sebelumnya 223 | Screen Shot 2021-10-23 at 15 40 34 224 | 225 | 16. Buka Directory target dan open file ini pada chrome 226 | Screen Shot 2021-10-23 at 15 41 02 227 | Screen Shot 2021-10-23 at 15 41 34 228 | 229 | 17. Maka hasil dari generate test tersebut adalah seperti ini: 230 | Screen Shot 2021-10-23 at 15 41 42 231 | 232 | Selamat Mencoba :) 233 | -------------------------------------------------------------------------------- /driver/chromedriver: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wisnuwm/automation-bdd-google/f7aff946c003e44f1cdd8103c261da7cbe4db45b/driver/chromedriver -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.example 8 | automation-bdd-google 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 15 13 | 15 14 | 15 | 16 | 17 | 18 | 19 | io.cucumber 20 | cucumber-java 21 | 6.9.0 22 | 23 | 24 | 25 | 26 | junit 27 | junit 28 | 4.13.1 29 | test 30 | 31 | 32 | 33 | 34 | io.cucumber 35 | cucumber-junit 36 | 6.9.0 37 | test 38 | 39 | 40 | 41 | 42 | org.seleniumhq.selenium 43 | selenium-java 44 | 3.141.59 45 | 46 | 47 | 48 | 49 | net.masterthought 50 | cucumber-reporting 51 | 5.4.0 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | org.apache.maven.plugins 61 | maven-surefire-plugin 62 | 2.22.0 63 | 64 | true 65 | 66 | 67 | 68 | net.masterthought 69 | maven-cucumber-reporting 70 | 2.8.0 71 | 72 | 73 | execution 74 | verify 75 | 76 | generate 77 | 78 | 79 | automation-bdd-google 80 | ${project.build.directory}/cucumber-report-html 81 | ${project.build.directory}/cucumber.json 82 | 83 | 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /src/test/java/StepDef/SearchGoogle.java: -------------------------------------------------------------------------------- 1 | package StepDef; 2 | 3 | import io.cucumber.java.en.And; 4 | import io.cucumber.java.en.Given; 5 | import io.cucumber.java.en.Then; 6 | import io.cucumber.java.en.When; 7 | import org.openqa.selenium.By; 8 | import org.openqa.selenium.Keys; 9 | import org.openqa.selenium.WebDriver; 10 | import org.openqa.selenium.chrome.ChromeDriver; 11 | import org.openqa.selenium.chrome.ChromeOptions; 12 | 13 | public class SearchGoogle { 14 | WebDriver driver; 15 | @Given("I Open browser") 16 | public void iOpenBrowser() { 17 | final String dir = System.getProperty("user.dir"); 18 | System.out.println("current dir = " + dir); 19 | System.setProperty("webdriver.chrome.driver", dir+"/driver/chromedriver"); 20 | driver = new ChromeDriver(); 21 | } 22 | 23 | @And("Open website Google") 24 | public void openWebsiteGoogle() throws InterruptedException { 25 | driver.get("https://www.google.co.id/"); 26 | Thread.sleep(1000); 27 | } 28 | 29 | @And("Located on google website") 30 | public void locatedOnGoogleWebsite() { 31 | driver.findElement(By.name("btnK")).isDisplayed(); 32 | } 33 | 34 | @When("I search {string}") 35 | public void iSearch(String searchValue) { 36 | driver.findElement(By.name("q")).sendKeys(searchValue); 37 | driver.findElement(By.name("q")).sendKeys(Keys.ENTER); 38 | } 39 | 40 | @Then("Showing result related with {string}") 41 | public void showingResultRelatedWith(String result) { 42 | driver.findElement(By.xpath("//a[@href='https://id.linkedin.com/in/wisnuwm']")).isDisplayed(); 43 | String urlLinkedinWisnu = driver.findElement(By.xpath("//a[@href='https://id.linkedin.com/in/wisnuwm']")).getText(); 44 | System.out.println(urlLinkedinWisnu); 45 | driver.close(); 46 | driver.quit(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/TestRunner/TestRunner.java: -------------------------------------------------------------------------------- 1 | package TestRunner; 2 | 3 | import io.cucumber.junit.Cucumber; 4 | import io.cucumber.junit.CucumberOptions; 5 | import org.junit.runner.RunWith; 6 | 7 | @RunWith(Cucumber.class) 8 | @CucumberOptions(features="src/test/resources/Features", 9 | glue= {"StepDef"}, 10 | plugin ={"pretty","json:target/cucumber.json"}) 11 | public class TestRunner { 12 | } 13 | -------------------------------------------------------------------------------- /src/test/resources/Features/SearchGoogle.feature: -------------------------------------------------------------------------------- 1 | Feature: Search Google 2 | Scenario: I want to using feature search on google 3 | Given I Open browser 4 | And Open website Google 5 | And Located on google website 6 | When I search "Wisnu Munawar" 7 | Then Showing result related with "Wisnu Munawar" --------------------------------------------------------------------------------