Home page

Showing posts with label Maven. Show all posts
Showing posts with label Maven. Show all posts

Friday, November 29, 2019

Jenkins Install Suggested Plugins

Following plugins Jenkins recommend to install while configuring first time:
  1. Folders
  2. Timestamper
  3. Pipeline
  4. Git
  5. PAM Authentication
  6. OWASP Markup  Formatter
  7. Workspace Cleanup
  8. GitHub Branch Source
  9. Subversion
  10. LDAP
  11. Build Timeout
  12. Ant
  13. Pipeline: GitHub Groovy Libraries
  14. SSH Slaves
  15. Email Extension
  16. Credentials Binding
  17. Gradle
  18. Pipeline: Stage View
  19. Matrix Authorization Strategy
  20. Mailer

Wednesday, September 25, 2019

Data driven testing on Facebook using Selenium + JAVA

  1. Create maven project
  2. Convert project to TestNG
  3. Create pageobject, Utility and facebooktest packages
  4. Create Driver and TestData folders.
  5. Copy following code to facebooktest > AppTest.java file
  6. package com.wallethub.faceboook;

    import java.io.FileInputStream;
    import java.io.IOException;

    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.DataFormatter;
    import org.testng.annotations.DataProvider;
    import org.testng.annotations.Test;

    import pageobject.HomePageObject;
    import pageobject.LoginPageObject;
    import utilities.BaseClass;

    public class AppTest extends BaseClass{
       
        public static String file_location = System.getProperty("user.dir")+"//TestData//Login.xls";
        public static HSSFWorkbook workbook;
        public static HSSFSheet worksheet;
        static String SheetName= "Sheet1";
        public static DataFormatter formatter= new DataFormatter();
       
        LoginPageObject LPO = new LoginPageObject();
        HomePageObject HPO = new HomePageObject();
       
        @Test(dataProvider="readTestData")
        public void testApp(String UserName, String Password) throws InterruptedException{
            LPO.login(UserName, Password);
            HPO.post();
        }
       
        @DataProvider(name="readTestData")
        public static Object[][] ReadVariant() throws IOException
        {
        FileInputStream fileInputStream= new FileInputStream(file_location);
            workbook = new HSSFWorkbook (fileInputStream);
            worksheet=workbook.getSheet(SheetName);
            HSSFRow Row=worksheet.getRow(0);       
        
            int RowNum = worksheet.getPhysicalNumberOfRows();
            int ColNum= Row.getLastCellNum(); 
            
            Object Data[][]= new Object[RowNum-1][ColNum];
            
                for(int i=0; i<RowNum-1; i++)
                { 
                    HSSFRow row= worksheet.getRow(i+1);
                    
                    for (int j=0; j<ColNum; j++)
                    {
                        if(row==null)
                            Data[i][j]= "";
                        else
                        {
                            HSSFCell cell= row.getCell(j);
                            if(cell==null)
                                Data[i][j]= ""; 
                            else
                            {
                                String value=formatter.formatCellValue(cell);
                                Data[i][j]=value;
                            }
                        }
                    }
                }

            return Data;
        }
    }
  7. Copy following code into pageobject > LoginPageObject.java file
  8. package pageobject;

    import org.openqa.selenium.By;
    import org.openqa.selenium.support.PageFactory;

    import utilities.BaseClass;

    public class LoginPageObject extends BaseClass{
       
        public LoginPageObject() {
            PageFactory.initElements(driver, this);
        }
       
        By username = By.id("email");
        By password = By.id("pass");
        By loginBTN = By.xpath("//input[contains(@value,'Log In')]");
        public void login(String UserName, String Password){
            driver.findElement(username).sendKeys(UserName);
            driver.findElement(password).sendKeys(Password);
            driver.findElement(loginBTN).click();
        }
    }
  9. Copy following code into Utility > BaseClass.java file
  10. package utilities;

    import java.util.HashMap;
    import java.util.Map;

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Optional;
    import org.testng.annotations.Parameters;

    public class BaseClass {
       
        protected static WebDriver driver;
        Map<String, Object> prefs = new HashMap<String, Object>();
        ChromeOptions options = new ChromeOptions();
       
        @Parameters("browser")
        @BeforeTest(alwaysRun=true)
        public void setupWebdriver(@Optional ("chrome") String browser){
            browserLaunch(browser);
        }

        public void browserLaunch(String browser){
           
            prefs.put("profile.default_content_setting_values.notifications", 2);
            if(browser.equals("chrome")){
                options.setExperimentalOption("prefs", prefs);
                System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"//drivers//chromedriver.exe");
                driver = new ChromeDriver(options);
                driver.manage().window().maximize();
            }
           
            driver.get("https://www.facebook.com/");
        }
    }
  11. Copy following code into Utility > Tools.java file
  12. package utilities;

    import org.openqa.selenium.By;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;

    public class Tools extends BaseClass {
       
        static WebDriverWait wait;
       
        public static void waitForElementToBePresent(By by, int timeOutSeconds) {
            wait = new WebDriverWait(driver, timeOutSeconds);
            wait.until(ExpectedConditions.presenceOfElementLocated(by));
        }

    }
  13. Create Login.xls file under TestData folder with credentials
  14. Copy following dependencies in POM.xml file
  15. <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
                <version>3.141.59</version>
            </dependency>

            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>6.14.3</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.uncommons</groupId>
                <artifactId>reportng</artifactId>
                <version>1.1.4</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>4.1.0</version>
            </dependency>

            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>4.1.0</version>
            </dependency>
        </dependencies>
  16. Copy following code into TestNG.xml file
  17. <suite name="Suite">
      <test thread-count="5" name="Test">
      <parameter name="browser" value="chrome"/>
        <classes>
          <class name="faceboooktest.AppTest"/>
        </classes>
      </test> <!-- Test -->
    </suite> <!-- Suite -->

Thursday, September 12, 2019

Not getting missing package specific import suggestions in feature file for @Given / @When / @Then annotations ?

Reason: Dependency (cucumber-java & cucumber-jvm-deps) are missing in pom.xml file.

Solution:
1. Go to https://mvnrepository.com/
2. Search with cucumber-java
3. Select info.cukes » cucumber-java
4. Click latest version (1.2.5)
5. Copy 

    <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.2.5</version>
    </dependency>
6. Paste in pom.xml
7. Search with cucumber-jvm-deps
8. Select info.cukes » cucumber-jvm-deps
9. Click latest version (1.0.5)
10. Copy  

     <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-jvm-deps</artifactId>
            <version>1.0.5</version>
            <scope>provided</scope>
    </dependency>
11. Paste in pom.xml

Friday, July 19, 2019

Maven with cucumber

Practice URL: https://www.techlistic.com/p/selenium-practice-form.html

Editor: Eclipse


Follow below mentioned steps:


  1. Select Maven project to create new project
  2. Click Next on “Select project name and location” popup
  3. Click Next on “Select an Archetype” popup.
  4. Enter “AboutMe_techlistic_cucumber” in Group Id & Artifact Id and click Finish button.
  5. Right click project > Maven > Update Project: Check “Force Update of Snapshots/Release” checkbox and click OK button. Follow this step to avoid any conflict related to maven.
  6. Delete packages from src/main/java and src/test/java
  7. Create “Features” folder under project.
  8. Create “drivers” folder under project.
  9. Create 3 packages under src/test/java: a) pageObjects b) utilities c) stepDefinitions
  10. Right click “Fetures” folder > create new file. Give filename AboutMe.feature (Popup will open “Better editor support for '*.feature' files is available on the Marketplace.”)
  11. Install Cucumber Eclipse Plugin.
  12. Write Feature and scenario in AboutMe.feature file as follow
Feature: About Me

Scenario: Fill About Me form successfully
Given User Launch Chrome browser
When User opens URL "https://www.techlistic.com/p/selenium-practiceform.html"
And User enters Firstname as "sABC" and Lastname as "sXYZ" and Gender as "Male" and Experience as "7" and Date as "06-05-1983" and Profession as "Automation Tester" and AutomationTools as "Selenium Webdriver" and Continents as "Asia" and SeleniumCommand as "Wait Commands"
And Click on Button
And close browser



13. Create AboutMePage.java file under pageObjects package and copy paste following code:
package pageObjects;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;

public class AboutMePage {
               
                public WebDriver ldriver;
               
                public AboutMePage(WebDriver rdriver){
                                ldriver = rdriver;
                                PageFactory.initElements(rdriver, this);
                }
               
               
               
                By txtFirstName = By.name("firstname");
                By txtLastName = By.name("lastname");
                By genderMale = By.id("sex-0");
                By genderFemale = By.id("sex-1");
                By experience7 = By.id("exp-6");
                By date = By.id("datepicker");
                By prefession = By.id("profession-1");
                By tool = By.id("tool-2");
                By continent = By.id("continents");
                By submit = By.id("submit");
               
                //Action methods
                public void setFirstName(String FirstName){
                                ldriver.findElement(txtFirstName).sendKeys(FirstName);
                }
               
                public void setLastName(String LastName){
                                ldriver.findElement(txtLastName).sendKeys(LastName);
                }

                public void setGender(String gender_value){
                                if(gender_value.equals("Male"))
                                                ldriver.findElement(genderMale).click();
                                else if(gender_value.equals("Female"))
                                                ldriver.findElement(genderFemale).click();
                }
               
                public void setExperience(String experience){
                                if(experience.equals("7"))
                                                ldriver.findElement(experience7).click();
                }
               
                public void setDate(String date_value){
                                ldriver.findElement(date).sendKeys(date_value);
                }
               
                public void setProfession(String profession_valule){
                                if(profession_valule.equals("Automation Tester"))
                                                ldriver.findElement(prefession).click();
                }
               
                public void setTool(String tool_value){
                                if(tool_value.equals("Selenium Webdriver"))
                                                ldriver.findElement(tool).click();
                }
               
                public void setContinet(String continet){
                                Select drpContinent = new Select(ldriver.findElement(continent));
                                drpContinent.selectByVisibleText(continet);
                }

                public void clickSubmit(){
                                ldriver.findElement(submit).click();
                }
}

14. Create steps file under “stepDefinitions” package with name “steps_AboutMe.java”.
15. Provide following dependencies in POM.xml
<dependencies>
              <dependency>
                     <groupId>junit</groupId>
                     <artifactId>junit</artifactId>
                     <version>4.12</version>
                     <scope>test</scope>
              </dependency>

              <dependency>
                     <groupId>io.cucumber</groupId>
                     <artifactId>cucumber-core</artifactId>
                     <version>4.6.0</version>
              </dependency>

              <dependency>
                     <groupId>io.cucumber</groupId>
                     <artifactId>cucumber-html</artifactId>
                     <version>0.2.7</version>
              </dependency>

              <dependency>
                     <groupId>net.sourceforge.cobertura</groupId>
                     <artifactId>cobertura</artifactId>
                     <version>2.1.1</version>
                     <scope>test</scope>
              </dependency>

              <dependency>
                     <groupId>io.cucumber</groupId>
                     <artifactId>cucumber-java</artifactId>
                     <version>4.6.0</version>
              </dependency>

              <dependency>
                     <groupId>io.cucumber</groupId>
                     <artifactId>cucumber-junit</artifactId>
                     <version>4.6.0</version>
                     <scope>test</scope>
              </dependency>

              <dependency>
                     <groupId>io.cucumber</groupId>
                     <artifactId>cucumber-jvm-deps</artifactId>
                     <version>1.0.6</version>
                     <scope>provided</scope>
              </dependency>

              <dependency>
                     <groupId>net.masterthought</groupId>
                     <artifactId>cucumber-reporting</artifactId>
                     <version>4.8.0</version>
              </dependency>

              <dependency>
                     <groupId>org.hamcrest</groupId>
                     <artifactId>hamcrest-core</artifactId>
                     <version>2.1</version>
                     <scope>test</scope>
              </dependency>

              <dependency>
                     <groupId>org.seleniumhq.selenium</groupId>
                     <artifactId>selenium-java</artifactId>
                     <version>3.141.59</version>
              </dependency>

              <dependency>
                     <groupId>com.sun</groupId>
                     <artifactId>tools</artifactId>
                     <version>1.8</version>
                     <scope>system</scope>
                     <systemPath>C:\Program Files\Java\jdk1.8.0_221\lib\tools.jar</systemPath>
              </dependency>

</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
</plugin>
</plugins>

</build>



16. Open “AboutMe.feature” file and run program by right clicking in the file > Run As > Cucumber Feature
17. Console will print all missing test steps in the form of methods. Copy all methods and paste inside steps_AboutMe.java file
18. Step 16 & 17 is for your understanding how to get ready made methods.
19. Remove content of the steps_AboutMe.java and copy paste following code:
package stepDefinitions;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion.User;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import pageObjects.AboutMePage;

public class steps_AboutMe {
  
   public WebDriver driver;
   public AboutMePage AM;
  
   @Given("User Launch Chrome browser")
   public void user_Launch_Chrome_browser() {
          System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"//drivers/chromedriver.exe");
          driver = new ChromeDriver();
          AM = new AboutMePage(driver);
   }

   @When("User opens URL {string}")
   public void user_opens_URL(String url) {
       driver.get(url);
   }

   @When("User enters Firstname as {string} and Lastname as {string} and Gender as {string} and Experience as {string} and Date as {string} and Profession as {string} and AutomationTools as {string} and Continents as {string} and SeleniumCommand as {string}")
   public void user_enters_Firstname_as_and_Lastname_as_and_Gender_as_and_Experience_as_and_Date_as_and_Profession_as_and_AutomationTools_as_and_Continents_as_and_SeleniumCommand_as(String FirstName, String LastName, String gender_value, String experience, String date_value, String profession_valule, String tool_value, String continet, String string9) {
      AM.setFirstName(FirstName);
      AM.setLastName(LastName);
      AM.setGender(gender_value);
      AM.setExperience(experience);
      AM.setDate(date_value);
      AM.setProfession(profession_valule);
      AM.setTool(tool_value);
      AM.setContinet(continet);
   }

   @When("Click on Button")
   public void click_on_Button() {
          AM.clickSubmit();
   }

   @When("close browser")
   public void close_browser() {
      
   }

}

20. Create testRunner package under src/test/java
21. Create TestRun.java file under testRunner package and copy paste following code inside
package testRunner;

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions
   (
   features=".//Features/AboutMe.feature",
   glue="stepDefinitions",
   dryRun=false,
   monochrome=true, //remove unnecessary characters
   plugin={"pretty", "html:test-output"}
                
                
   )
public class TestRun {

}

22. Right click inside TestRun.java file > Run As > JUnit Test.