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.