Monday, 27 April 2020

SE - 77 - What are AJAX calls and how to handle it in Selenium

AJAX - Asynchronous Java-Script And XML

It is a way to Asynchronously send/receive data from a web page to server without actually re-loading the whole page. From a user perspective there is no visible change in the webpage as the request/response is done automatically without refreshing the webpage.
There may be issue in locating such elements as it may take some time to load.  So it needs to be handled as normal execution of script will fail if the AJAX calls are in process.

To handle this situation we have multiple options in Selenium :

1) Thread.Sleep
2) Explicit Wait
3) Implicit Wait
4) Fluent wait
5) Selenium based waits


Sunday, 26 April 2020

SE - 76 - Something about BDD/Cucumber

-> A Background is run before each scenario, but after any Before hooks. The order of the operations  : Before Hook 1 -> Before Hook 2 -> … -> Background -> Scenario -> .....-> After Hook2 -> After Hook 1

-> Before hooks will be run before the first step of each scenario. They will run in the same order of which they are registered. After hooks will be run after the last step of each scenario, even when there are failing, undefined, pending or skipped steps


->@Before and @After tagging a method with either of these will cause the method to run before or after each scenario runs. They will run in the same order of which they are registered.

->Both @Before and background runs before each scenario

->There can be only one Background in one Feature file and it allows us to set a precondition for all Scenarios in a Feature file.

->An important thing to note about the after hook is that even in case of test fail, after hook will execute for sure.

SE - 75 - Desired Capabilities in Selenium

Desired Capabilities are used in selenium to set the properties of specific browsers at the time of execution. There are multiple capabilities which we can set for browser like browser name , version , operating system , etc. These properties are in key value pairs. To declare Desired Capabilities in Selenium automation testing using Grid, we can use the setCapability method from the DesiredCapabilities class to set the different types of capabilities of the browser (Ex. Chrome, IE, Firefox, Edge) platform name (Ex. Windows, macOS, etc.).


Few important methods of Desired capability :

getCapability():This method getCapability() from the class Desired Capabilities, which can be used to get the capabilities of the current system which we are using.

setCapability(): This method setCapability() from the class Desired Capabilities, can be used to set the name of device, name of platform, version of platform, absolute path of the application which is under test, application activity (in Mobile automation), application Package (in Java) and etc.

getBrowserName(): This method getBrowserName() from the class Desired Capabilities, can be used to get the name of the Browser.

setBrowserName(): This method setBrowserName() from the class Desired Capabilities, can be used to set the name of the Browser.

getVersion() : This method getVersion() from the class Desired Capabilities, can be used to get the version of the browser or platform.

setVersion() : This method setVersion() from the class Desired Capabilities, can be used to set the version of the browser or platform.

getPlatform() : This method getPlatform() from the class Desired Capabilities, can be used to get the details of the platform.

setPlatform(): This method setPlatform() from the class Desired Capabilities, can be used to set the details of the platform.

There are generic methods provided by DesiredCapabilities. Apart from these capabilities there are other options too which can be provided specifically by browsers . For Chrome browser we have ChromeOptions , for firefox Browser we have FirefoxOptions and so on.

These browser specific classes also gives some additional option to be done with desired capabilities.Let's see some additional options of ChromeOptions class :

Disable-infobars: It is used to prevent chrome browser from displaying notifications like “Chrome is being controlled by automated software”.

Make-default-browser: It is used to make the chrome browser as default browser.

Disable-popup-blocking: It is used to disable the pop-ups which are displayed on chrome browser.

Incognito: It opens chrome browser in incognito mode

start -maximized: It opens chrome browser in maximized mode

Headless: It is used to open the chrome browser in headless mode.

Saturday, 25 April 2020

SE - 74 - Statement and ResultSet

Statement and Resultset are used in JDBC connections to run queries and store results. The results are then used further in the program.


JDBC Statement:

Statement is an interface. It provides some methods through which we can submit SQL queries to the database. It will be implemented by driver implementation providers. createStatement() method on Connection object will return Statement object.


JDBC ResultSet:

ResultSet also an interface and is used to retrieve SQL select query results. A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set. It provides getXXX() methods to get data from each iteration. Here XXX represents datatypes.

SE - 73 - Difference between : Type casting and type conversion

Type Conversion : This is done implicitly by JAVA for those conversions which are allowed to be done automatically.


Widening or Automatic Type Conversion
Widening conversion takes place when two data types are automatically converted. This happens when:
-> The two data types are compatible.
-> When we assign value of a smaller data type to a bigger data type.

In java the numeric data types are compatible with each other but no automatic conversion is supported from numeric type to char or boolean. Also, char and boolean are not compatible with each other. If the conversion is done in the following sequence it will be Widening/Automatic conversion

Byte -> Short -> Int -> Long -> Float -> Double
int i = 10
          
        // automatic type conversion
        long l = i; 
          
        // automatic type conversion
        float f = l; 


Type Casting :  This is same as Type conversion but here we need to explicitly type cast the conversion. It is not done automatically by JAVA.

Narrowing or Explicit Conversion

If we want to assign a value of larger data type to a smaller data type we perform explicit type casting or narrowing.

-> This is useful for incompatible data types where automatic conversion cannot be done.
-> Here, target-type specifies the desired type to convert the specified value to.

 Explicit Typecasting is done if the conversion is required in reverse order of implicit conversion :
Byte <- Short <- Int <- Long <- Float <- Double

  double d = 100.04
          
        //explicit type casting
        long l = (long)d;
          
        //explicit type casting 
        int i = (int)l; 





SE - 72 - Something about Frames !

About Frames :

1) We can get the count of all the frames available on a page by :
Int size = driver.findElements(By.tagName("iframe")).size();


2) To go to a particular frame we use switchTo() method in webdriver
driver.switchTo().frame(0);


3) There are 4 ways of locating a frame :
By frame ID : driver.switchTo().frame("frameID");
By frame Name : driver.switchTo().frame("frame name");
By frame Index: driver.switchTo().frame(0);
By frame Webelement : driver.switchTo().frame(WebElement);


4) Parentframe() is used to switch to immediate parent of frame
   defaultcontent() is used to switch to initial frame.

5) Handling multiple frames:
   i) If all the frames are ar same node/level : In this scenario we will have to return back to the parent        of node or default node as applicable . After that we need to switch to another frame.

  ii) If the frames are present in node and another frame is in the child of parent frame node : In this          case we can use switchTo() to switch to the first frame and then again we can use switchTo() to            switch to inner frames


Note : frame to frame switching is only possible is one frame is the child of the main frame

6) If any element is on frame and we are trying to locate it directly(without switching to frame) , then it will give NoSuchElementException


7) If invalid locator is given to locate a frame then NoSuchFrameException will hit.

Friday, 24 April 2020

SE - 67 - Difference between valueOf and parseInt !

INTEGER.PARSEINT()INTEGER.VALUEOF()
It can only take a String as a parameter.It can take a String as well as an integer as parameter.
It returns a primitive int value.It returns an Integer object.
When an integer is passed as parameter, it produces an error due to incompatible typesWhen an integer is passed as parameter, it returns an Integer object corresponding to the given parameter.
This method produces an error(incompatible types) when a character is passed as parameter.This method can take a character as parameter and will return the corresponding unicode.
This lags behind in terms of performance since parsing a string takes a lot of time when compared to generating one.This method is likely to yield significantly better space and time performance by caching frequently requested values.
If we need the primitive int datatype then Integer.parseInt() method is to be used.If Wrapper Integer object is needed then valueOf() method is to be used.

SE - 71 - Interface and its implementations in excel

We have 3 flavours of classes used in excel:

HSSF : To handle .xls type of excel(older versions of excel)

SXSSF : It is an API-compatible streaming extension of XSSF to be used when very large spreadsheets have to be produced, and heap space is limited

XSSF : To handle .xlsx type of excel(newer versions of excel)


Major Interfaces  and Its Implementations :

Row : HSSFRow, SXSSFRow, XSSFRow
Cell : HSSFCell, SXSSFCell, XSSFCell
Sheet : HSSFSheet, SXSSFSheet, XSSFChartSheet, XSSFDialogsheet, XSSFSheet
WorkBook : HSSFWorkbook, SXSSFWorkbook, XSSFWorkbook

SE - 70 - Difference between Click() and Submit() methods in selenium

Both the methods in selenium almost plays a similar role. Submit is specific to some forms in which the internal tag is designed in the way that it has acceptance to Submit() method.

submit()
Submits the element to the web server. If this current element is a form, or an element within a form then this will be submitted to the web server. If this causes the current page to change, then this method will block until the new page is loaded. StaleElementReferenceException is thrown when the target element is no longer valid in the document DOM.



click()
Click on the element. If the click causes a new page to load, the method will attempt to block until the page has loaded. After calling the method, you should discard all references to this element unless you know that the element and the page will still be present. Otherwise, any further operations performed on this element will have undefined behavior. ElementNotVisibleException is thrown when the target element is not visible. StaleElementReferenceException is thrown when the target element is no longer valid in the document DOM(document object model).

SE - 69 - Can constructor throw exception in JAVA ?

YES , Constructor can also throw exceptions like normal method does.


public class ConstructorTest {
   public ConstructorTest() throws InterruptedException {
      System.out.println("Before Sleep");
      Thread.sleep(1000);
      System.out.println("After Sleep");
   }

SE - 68 - To click on hidden link

There are some instances where link is present on DOM but it is hidden. We can't locate it normally and do any operation on it.
On locating it we get ElementnotVisibleException
It is generally made hidden by style="display:none;".

So to handle this situation we need to take support of JavaScriptExecutor.

Code Below :

Example 1 : 
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
Here element is the object of the element we are looking for.
Example 2 : 
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.getElementById('txt'.value='ABCD'");

Tuesday, 21 April 2020

SE - 66 - Some basics of Selenium!

1) flush() method is used to write any data to file and Length() method is used to get its length

2) If we are writing in preexisting file then it will be overrided. If we want to append it we neet to give Filewriter(name , true)

3) BufferReader/BufferedWriter is wrapper on FileReader/Writer which helps in transmission of data in packets

4) Assertion.assertAll is written at the end of each soft assert to display failed assertions with reasons

5)Implicit wait works on existance of element while Explicit works on existance of property

6) Type of exceptions in failure of implicit and explicit wait
Implicit wait : No such Element
Explicit Wait : TImeout exception

7) While testing with Selenium,we use setProperty method because the browser doesn’t have a built-in server to run the automation code. In this case, we will need a Chrome/IE/Gecko driver server for communicating your Selenium code to the browser. It allows the selenium code to interact with browser.

SE - 65 - Does Abstract class has constructors ?

Ans to this question is Yes . Abstract class can have constructors. Now the question is usage of it because we can't create object of Abstract Class. The abstract class constructor is called when in child class its methods are defined and they are called in another class. When we create an object of any subclass all the constructors in the corresponding inheritance tree are invoked in the top to bottom approach. The same case applies to abstract classes. 

SE - 64 - Output with combination of String , Integer and Character


Let's understand by a program :


public static void main(String[] args) {
  String S1 = " String1 ";
  String S2 = " String2 ";
  
  char c1 = 'a';
  char c2 = 'b';
  
  int i1 = 10;
  int i2 = 20;
  
System.out.println(S1 + c1 + i1);  // String1 a10
System.out.println(c1 + S1 + i1);  // a String1 10
System.out.println(S1 + i1 + i2);  // String1 1020
System.out.println(i1 + i2 + S1);  // 30 String1 
System.out.println(S1 + c1 + c2);  //  String1 ab
System.out.println(S1 + S2 + c1 + c2 + i1 + i2);  // String1  String2 ab1020
System.out.println(i1 + c1 + i2);  // 127
System.out.println(i1 + S1 + i2);  // 10 String1 20
System.out.println(S1 + c1 + S2);  //  String1 a String2 
System.out.println(S1 + i1 + S2);  // String1 10 String2 
System.out.println(S1 + i1 + i2 + S2);  // String1 1020 String2 
System.out.println(S1 + S2);  //  String1  String2 
System.out.println(c1 + c2);  // 195
System.out.println(S1 + c1);  //  String1 a
System.out.println(S1 + i1);  //  String1 10
System.out.println(i1 + c1);  // 107
System.out.println(i1 + c1 + S1);  // 107 String1 
System.out.println(c1 + c2 + S1);  // 195 String1 
}

Excerpts : 
1) If there is char and int combination then the ASCII value of char will be summed up with integer

2) If there is only integer , then it will be summed up.

3) If there is integer before String, then integer will be added up and will be concatenated with string.

4) If there is Integer after string  , then string will be concatenated with int. No addition will happen.

5) Char or Int prior to string will be summed up but if it is after the string it will not be summed up


SE - 63 - About access specifiers and Access Modifiers

Access Specifiers : public , private , protected , default(package)

Access Modifiers :  final , static , abstract


Apart from normal restrictions and behaviors of above access specifiers let's see some more :

1) A local variable can't be static but it can be final.

2) A variable can never be abstract

3) Overridden method can't be more restrictive than the base class's method

4) A class can't be static (inner class can be static)

5) Interface contains only final variables(constants)

6) Final :
Variable is constant
Method can't be overridden
class can't be inherited

7) Abstract class can have :
All declared methods only
All defined methods only
Declared and defined methods combinations

8) Top level class in Java can't be private or protected or static , only public and default are allowed. Inner class can be private / static.

9) If we declare final static  variable at class level , we can't modify it in any methods.

10) If we declare only static variable at class level , we can modify it in any methods.

11) It is not required to mark a method as abstract. A abstract method is a method without body in abstract class

12) All wrapper class are final



Monday, 20 April 2020

SE - 62 - How to exclude a dependency in Maven and how to make a dependency optional ?

Sometimes there is requirement to make a dependency optional or exclude that particular dependency. We can achieve it by manually deleting it from .m2 folder . There is another efficient way to do it through pom.xml file by using particular tags. Lets see how :


1) To make a dependency Optional :

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.seleniumhq</groupId>
  4. <artifactId>selenium</artifactId>
  5. <version>1.0</version>
  6. <optional>true</optional> <!-- we set value as true or false only -->
  7. </dependency>
  8. </dependencies>
Here we are simply using a optional tag in particular dependency and set the optional tag either as true or false.

2) To exclude a particular dependency :
  1. <exclusions>
  2. <exclusion>
  3. <groupId>org.seleniumhq</groupId>
  4. <artifactId>selenium</artifactId>
  5. </exclusion>
  6. </exclusions>
Here we can add the dependencies that can be excluded from the dependency by using this

SE - 61 - Collection Vs Collections

Collection is an interface and Collections is a class but both comes under Collection framework. Many of the classes of Java Collection Framework inherits Collection.

Collections is an utility class in java.util package. It consists of only static methods which are used to operate on objects of type Collection. Few of the examples of Collections class methods are :

synchronizedCollection() - It is used to get a synchronized (thread-safe) collection backed by the specified collection.

addAll() - It is used to adds all of the specified elements to the specified collection.


sort() - Used to sort a given collection


copy() - It is used to copy all the elements from one list into another list.


SE - 60 - NotFoundException in Selenium and its subclasses

The NotFoundException is a super class which includes the subclass NoSuchElementException. The known direct subclasses of NotFoundException are: NoAlertPresentException, NoSuchContextException, NoSuchElementException, NoSuchFrameException and NoSuchWindowException. So if we want one catch statement to catch all five exception and they will all be handled the same way then we can just handle NotFoundException.