Selenium May Cycle Qna

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

9

https://www.tutorialspoint.com/can-we-declare-a-constructor-as-private-in-java

Yes, we can declare a constructor as private. If we declare a constructor as private we are not able to create an object of a class.
We can use this private constructor in the Singleton Design Pattern.

Conditions for Private Constructor

 A private constructor does not allow a class to be subclassed.


 A private constructor does not allow to create an object outside the class.
 If all the constant methods are there in our class we can use a private constructor.
 If all the methods are static then we can use a private constructor.
 If we try to extend a class which is having private constructor compile time error will occur.
6

https://www.geeksforgeeks.org/replacing-public-with-private-in-main-in-java/
7

8
5

https://www.scientecheasy.com/2020/07/click-on-webelement.html/
4

http://makeseleniumeasy.com/2017/05/07/how-to-load-a-url-in-browser-without-using-get-or-navigate-
method/

package MakeSeleniumEasy;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class LoadURLWithoutGetNavigate {

public static void main(String[] args) {

System.out.println("Execution Starts");

// Setting chrome driver property and opening chrome browser


System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");

WebDriver driver= new ChromeDriver();

System.out.println("Browser opened.");

// We need to downcast WebDriver reference varaible to use JavascriptExecutor methods

JavascriptExecutor js= (JavascriptExecutor) driver;

String url = "https://www.google.com";

String script = "window.location = \'"+url+"\'";

js.executeScript(script);

System.out.println(driver.getCurrentUrl());

driver.quit();

}
39
Var chromeOptions=webdriver.Capabilitites.chrome();

chromeOptions[

binary: \/Applications/GoogleChrome.app/Contents/MacOS/Google Chrome\,

args:[],

extensions: [‘EXTENSION_LOCATION.crx’]

var driver = new webdriver.Builder().

withCapabilities(chromeOptions).

Build();

]
38

Ajax applications
37
34

35

We can check if an image is displayed on page with Selenium. To verify an image we shall take the help
of Javascript Executor. We shall utilize executeScript method to execute the Javascript commands in
Selenium.
Then pass the command return arguments[0].complete && typeof arguments[0].naturalWidth !=
\"undefined\" && arguments[0].naturalWidth > 0 as a parameter to that method. Moreover, the
statement import org.openqa.selenium.JavascriptExecutor has to be added in code.

Syntax

WebElement i = driver.findElement(By.xpath("//img[@title='Tutorialspoint']"));

Boolean p = (Boolean) ((JavascriptExecutor)driver)


.executeScript("return arguments[0].complete "
+ "&& typeof arguments[0].naturalWidth != \"undefined\" "
+ "&& arguments[0].naturalWidth > 0", i);
Let us check if the below image is present on page −

Example

Code Implementation.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
public class ImageChk{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
String url = "https://www.tutorialspoint.com/index.htm";
driver.get(url);
//identify image
WebElement i = driver.findElement
(By.xpath("//img[@title='Tutorialspoint']"));
// Javascript executor to check image
Boolean p = (Boolean) ((JavascriptExecutor)driver) .executeScript("return arguments[0].complete " +
"&& typeof arguments[0].naturalWidth != \"undefined\" " + "&& arguments[0].naturalWidth > 0", i);

//verify if status is true


if (p) {
System.out.println("Logo present");
} else {
System.out.println("Logo not present");
}
driver.quit();
}
}

36

https://www.guru99.com/selenium-core-
extensions.html#:~:text=To%20create%20user%20extension%20for,prototype%20and%20PageBot%20o
bject%20prototype
33
32

https://www.toolsqa.com/testng/testng-parallel-execution/

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >


<suite name = "Parallel Testing Suite">
<test name = "Parallel Tests" parallel = "methods" thread-count = "2">
<classes>
<class name = "ParallelTest" />
</classes>
</test>
</suite>
31

28
https://www.guru99.com/run-failed-test-cases-in-testng.html

Right-click on testng-failed.xml file and click on run as and select the option
called “testNG suite”.

Suppose if you have three test cases if all the test cases are executed successfully means you are not able to see this folder under
the test-output folder. This folder will appear only when one of the test case is failed. Then run this file, it will going to run only failed
test cases.
29

HTML Format
30

https://www.guru99.com/ssl-certificate-error-handling-selenium.html
27

26

Summary:

 SSL (Secure Sockets Layer) is a standard security protocol for establishing secure connection between the
server and the client
 Browser and the server use SSL Certificate mechanism to be able to establish a secure connection.
 SSL works through a combination of programs and encryption/decryption routine that exist on the web
server computer and web server browser.
 When secure connection is not established between the server and client due to certificate SSL certificate
error will occur
 Need to adjust our script in such a way that it will take care of SSL Exception/error by itself through
Selenium Web driver.https://www.guru99.com/ssl-certificate-error-handling-selenium.html

23
24

We can read Javascript variables with Selenium webdriver. Selenium can run Javascript commands with the help
of executeScript method. The Javascript command to be executed is passed as an argument to the method. Also
we have to add the statement import org.openqa.selenium.JavascriptExecutor to work with Javascript.

Syntax
JavascriptExecutor j = (JavascriptExecutor) driver;
j.executeScript("return document.title")
Let us obtain the browser title of the below page by reading the value from Javascript variable. The output should
be About Careers at Tutorials Point – Tutorialspoint.
JavascriptExecutor j = (JavascriptExecutor) driver;
// get the browser title with document.title
String t = (String)j.executeScript("return document.title");

22
21

I am assuming that you have similar numbers on a web page and you want to verify that these numbers are in sorted order or not by using selenium.

Here is my take on this. you can find these elements and then make 2 similar arrays by getting the numbers in these elements. Sort one of these arrays and then
check if the sorted array is equal to non sorted array.
here is code snippet to do the same
List<WebElement> priceElements = driver.findElements(By.xpath("xpath for the elements"));
int n = priceElements.size();
Double[] priceArray = new Double[n];
Double[] priceArrayCopy = new Double[n];
for(int i=0;i<n;i++) {
priceArray[i] = Double.parseDouble(priceElements.get(i).getText());
priceArrayCopy[i] = Double.parseDouble(priceElements.get(i).getText());
}
Arrays.sort(priceArray);
Assert.assertTrue(Arrays.equals(priceArray, priceArrayCopy));
20

17
18

https://www.tutorialspoint.com/how-to-count-the-number-of-rows-in-a-table-in-selenium-with-
python#:~:text=We%20can%20count%20the%20total,of%20rows%20will%20be%20returned.

19

https://www.tutorialspoint.com/Java-string-length-without-using-length-method
15

You can use driver. manage(). timeouts(). pageLoadTimeout(); to handle your network latency in selenium 15
14
12

13
11

You might also like