Identify duplicate elements using HashSet and HashMap in Java

This program that you see is using HashSet and HashMap which is considered better than using 2 for loops to do the same job.

Using HashSet >> Stores unique values.

import java.util.HashSet;
import java.util.Set;

public class DuplicateElements {
  public static void main(String[] args) {
  String names[] = {"Java", "C", "C++", "Golang", "Ruby", "C"};
      Set<String> s = new HashSet<String>();
      for (String name : names) {
if (s.add(name) == false) {   System.out.println("Duplicate element:"+ name);
}
     }
   }
}

Using HashMap

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class DuplicateElements {

public static void main(String[] args) {
String names[] = {"Java", "C", "C++", "Golang", "Ruby", "C++"};
Map<String, Integer> s = new HashMap<String, Integer>();
for (String name: names) {
Integer count = s.get(name);
if (count == null) {
s.put(name, 1);
}
else {
s.put(name, ++count);
}
}
//get values from this HashMap
Set<Entry<String, Integer>> e = s.entrySet();
for (Entry<String, Integer> entry: e) {
if (entry.getValue()>1) {
System.out.println("Duplicate Item:" + entry.getKey());
}
}
}
}

String vs StringBuffer vs StringBuilder

String:

  • A string is immutable.
  • Stored in the Constant String Pool.
  • Every immutable object in Java is ThreadSafe.
StringBuffer: 
  • StringBuffer is mutable, it can be changed after it is defined. 
  • Its object is stored in heap. 
  • It is also Thread Safe. 
  • Performance is slow because of thread safety. (Note: Thread Safe is nothing but no two threads can simultaneously access the same method).
StringBuilder
  • StringBuilder is same as StringBuffer. Value can be changed and the object is stored in heap. 
  • The only difference is that, it is not Thread Safe. 
  • It is better in terms of performance because it is not thread-safe and there are no checks done to verify thread safety.

How to structure Selenium tests with less maintenance

Here are few things to structure Selenium tests with easy to maintain, reliable automated tests.
Building your Selenium tests using the Page Object pattern
For more on Page Object model click here.
Map your controls in a single place
Your controls should be mapped into a single location, so that if control changes, you only need to change it in a single location. Having it in a single location allows you to make a one-line code change if a developer re-maps the controls.
Abstract the test automation tool where possible
There are usually three actions that are performed most of the times - InvokeSetvalue, and GetValue. Invoke will click a link or button, check or uncheck a checkbox etc. SetValue will put values into controls, like putting text into a text box. GetValue gets data from the controls, which is primarily used for verification.

Use regular expressions to map your control names
Using regular expressions lets you define the control in a way that the developer can move it anywhere on the page and your tests will still run.
Create test data objects to hold your test data
Creating additional tests in some cases can be as simple as using an existing test with different data.
Separate the test from the implementation
Separation of concerns is a key technical design principle that you should apply from your tests. The intent of the test case should be separated from the physical implementation that does the interaction with the page as much as possible.

Where will the 'assert' message be displayed

Sample code:
package ABC;
import org.testng.Assert;
import org.testng.annotations.Test;
public class Assertcheck 
{
    @Test
    public void check() {
        Assert.assertTrue(true, "testing my website");
    }
}

Message parameter "testing my website" will get executed only if the Assert condition gets false. The message will appear in console.
But in the case of Assert condition passed, the message part will not get executed so you will not see any message in that case.
If you want to see the message in both the cases, you should use try-catch statement, something like:
try{
     Assert.assertTrue(true, "testing my website");
     //print your message for the case assert pass and/or perform any other event
}catch (Exception e){
     //print your message for the case assert fails and/or perform any other event
     loggerObj.debug("Assert Failed "+e.getMessage());
}

Unexpected error launching Internet Explorer

Error Message:
Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones. (WARNING: The server did not provide any stacktrace information)
Error message while launching IE browser. (using Selenium)

Solution:
The issue is something to do with the browser settings.
1. The browser has to be zoomed at 100%
2. In all 4 zones (Internet, Local intranet, Trusted Sites and Restricted sites), the "Enable Protected Mode" should be enabled.

Once enabled, click on Apply and restart your browser.
Now run the selenium script.

Page Object Model


Page Object Model -- Another important point to be considered while building the framework.
We many not give much of an importance while developing initially while automating the smaller application, but as the automation coverage grows, Page object model plays an important role.
Below is the presentation on POM that I had covered recently, and the topics covered include
  • What is POM?
  • Why POM?
  • Page Libraries
  • Syntax
  • Code 
  • Page Factory
  • Annotation
Here are few reasons why to consider using POM.

  • Page Object Model is an Object repository design pattern in Selenium WebDriver.
  • POM creates our testing code maintainable, reusable.
  • Operations and flows in the UI should be separated from verification which makes our code cleaner and easy to understand.
  • Object repository should be independent of test cases, use the same object repository for a different purpose with different tools.
  • Example: we can integrate POM with TestNG/JUnit for functional testing and at the same time with JBehave/Cucumber for acceptance testing.
  • Code becomes less and optimized because of the reusable page methods in the POM classes.
  • Methods get more realistic names which can be easily mapped with the operation happening in UI. i.e. if after clicking on the button we land on the Travel, the method name will be like 'gotoTravel ()‘
  • Page Factory is an optimized way to create object repository in POM concept.

Auto Tag of software requirements in Rational ReqPro

Manually tagging each of the business requirements to other forms of requirements like functional requirements or tagging the test cases to their respective requirements manually is a tedious task. There are many projects in different companies where they use Rational RequisitPro tool to tag each of their requirements manually. In order to save time (and money), there was an automation script developed. There has to be a template that the business requirements has to follow. Once we have the requirements as per the templates, all that is to be done is import the requirement and run the script. The presentation shows in detail as to how this is done. After executing the script, all the test cases are tagged to their respective business requirement.