Wednesday, May 6, 2015

TestNG parameters

TestNG Parameterization is used to pass parameters to tests as arguments. This is supported by using the TestNG @Parameters annotation.

1. Using @Parameters

2. Using @DataProvider


We can pass string types using @Parameters annotation to the test methods at run-time.

@Parameters ({"param-name"})
@Test
public void TestOne(String param){
}


@Parameters annotation can be used with the following annotation methods:

@BeforeXXX
@AfterXXX
@Factory
@Test

@Parameters can be used to initialize variables and use them in a class or test or maybe for the whole test suite execution.

How to handle multiple windows in Selenium?

Multiple web windows or tabs can be handled using window handlers and javascript executor.

Window handle is a unique identifier of the browser window. 

String strCurrentHandle = driver.getWindowHandle() : To get the window handle of the current browser window

Set<String> strHandles = driver.getWindowHandles() : To get all the window handles of browser windows opened. It returns set of handles.

String strPrevHandle = driver.getWindowHandle();
driver.findElement(By.xpath("//a[@id='link']");
Set<String> strHandles = driver.getWindowHandles();
for(String strHandle : strHandles){
    if(! strHandle.equalIgnoreCase(strPrevHandle)){
        driver.switchTo.window(strHandle)
        System.out.println(driver.getTitle());
        driver.close();
    }
}

driver.switchTo.window(strPrevHandle);

How to handle Alerts and Popups in Selenium?

 Handling alert

1. accept()

driver.switchTo().alert().accept();

2. dismiss()

driver.switchTo().alert().dismiss();

3. sendKeys()

driver.switchTo().alert().sendKeys("hello");

4. getText()

driver.switchTo().alert().getText();


Tuesday, May 5, 2015

How to run Cucumber from CommandLine

 

Running the tests or features from the command line or terminal without using the IDE or GUI.

Why

No dependency on IDE or GUI

Useful in integrations with other processes like CI/CD or deployment or release process. It will be easier and faster with command-line execution. It also consumes less memory.

When:

Whenever using CI tools like Jenkins
Whenever you need any batch or scheduled execution

Open command line
Navigate to project directory (cd "path")
mvn test

By default mvn test runs the files with naming syntax as below:
**/*Test*.java
**/Test*.java
**/OrderTest.java

Hence keep this naming convention only for the cucumber runner file. It will be run when execute the command mvn test from the command line.

CucumberOptions from command line:

1. Running a feature

mvn test -Dcucumber.options="src\test\resources\features\demo.feature"

2. Running a scenario in a feature file

mvn test -Dcucumber.options="src\test\resources\features\demo.feature:6"

3. Running with tags:

mvn test -Dcucumber.options="--tags @regression"

Or

mvn test -Dcucumber.options="-t @regression"

4. Reports

mvn test -Dcucumber.options="--plugin html:target/HtmlReport"

Or

mvn test -Dcucumber.options="-p html:target/HtmlReport"

5. Multiple options

mvn test -Dcucumber.options="src\test\resources\features\demo.feature:6" -Dcucumber.options="-p html:target/HtmlReports"


Background in Cucumber

Background in Cucumber is used to define a step or series of steps that are common across tests or scenarios within the feature file. 

A Background steps is run before every scenario in the feature file.

For example, two scenarios are listed below:

Scenario 1: Purchase order creation
Step 1: Launch the eCommerce website
Step 2: Login into website
Step 3: Search for a product
Step 4: Add to Cart
Step 5: Payment
Step 6: Submit order

Scenario 2: Update existing Purchase order
Step 1: Launch the eCommerce website
Step 2: Login into website
Step 3: Search for existing order
Step 4: Add new product
Step 5: Submit order

Step 1 and Step 2 are common steps in these two scenarios. So It recommends defining these common steps under the Background section, which will be run for each scenario in the feature file. 

Feature: Standard Purchase order 

Background:
Given Launch the eCommerce website
And Login into website
Scenario: Purchase order creation
Then Search for a product
And Add to Cart
And Payment
When Submit order
Scenario: Update existing Purchase order
Then Search for existing order
And Add new product
When Submit order

Monday, May 4, 2015

Scroll Operations using Javascript Executor in Selenium WebDriver

  1. ScrollIntoView
  2. ScrollTo
  3. ScrollBy

scrollIntoView() method scrolls the element's parent container such that the element on which scrollIntoView() is called is visible to the user.

WebElement footer = driver.findElement(By.cssSelector("#footerContainer"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView()", footer);

Zoom in and out using Selenium

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("document.body.style.zoom = '150%';");

js.executeScript("document.body.style.zoom = '70%';");

js.executeScript("document.body.style.zoom = '100%';");

Selenium 3 vs Selenium 4