Quick Reference - Selenium Interview Questions

* What is hard asserstion and soft assertion

* Handle alerts  -->  driver.switchTo().alert().accept();

* Select options

Select select = new Select(webElement); //select.getOptions();
select.selectByIndex(1);

* tesng.xml example
   <suite name="TestSuiteName" parallel="tests">
     <test name="TestName" preserve-order="true">
              <classes>
<class name="org.pacakge.name.TestClassName" />
     </classes>
     </test>
   </suite>

* how to disable web security in chrome
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-web-security");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);

* how to take screenshot
TakesScreenshoot screenshot = ((TakesScreenshot)driver)
File sourceFile = screenshot.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(sourceFile, "filename.jpg");

* Difference between navigate().to(URL) and driver.get(URL)

* Difference between implicitWait and explicitWait

* Wait for pageLoad
WebDriverWait wait = new WebDriverWait(driver, timeoutInSecs);
ExpectedCondition<Boolean> pageLoadCondition = new ExpectedCondition<Boolean>(){
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor)driver).executeScript(
"return document.readyState").equals("Complete");
}
};
wait.until(pageLoadCondition);


* KeyboardActions
Ex: Actions action = new Actions(driver);
action.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();

* Difference between testNG and JUnit

* assertions and types

* Run TestNG using cucumber
use following classes
MultiLoader()
RuntimeOptionsFactory()
PluginFactory()
ResourceLoaderClassFinder()

* ErrorCollector in JUnit

* Javascript in selenium

Ex: String MAX_BROWSER_WIN = "if (window.screen){window.moveTo(0,0); window.resizeTo(window.screen.availWidth, window.screen.availHeight);};";
((JavascriptExecutor)driver).executeScript(MAX_BROWSER_WIN);

Ex: ((JavascriptExecutor)driver).executeScript("scroll(250, 0)");

* configuration of testng in maven
<pluings>
<plugin>
-----
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>

* configuration of reports in maven

* grouping from testng.xml

<suite>
<test name="testName">
<groups>
<define name="includeGroup">
<inlucde name="includeTestOne" />
<include name="includeTestTwo" />
</define>
<define name="exlcudeGroup">
<exclude name="excludeTestOne" />
<exclude name="excludeTestTwo" />
</define>
<run>
<include name="include-group" />
<exclude name="exclude-group" />
</run>
</groups>
</test>
</suite>

* customization of test execution reports
using XSLT reports using testng+ANT

* Design patterns in selenium
DomainDrivenDesign: Express your tests in the language of the end-user of the app.
PageObjects  : A simple abstraction of the UI of your web app.
LoadableComponent : Modeling PageObjects as components.
BotStyleTests  : Using a command-based approach to automating tests, rather than the object-based approach that PageObjects encourage
AcceptanceTests   : Use coarse-grained UI tests to help structure development work.
RegressionTests   : Collect the actions of multiple AcceptanceTests into one place for ease of maintenance.

Capture network calls and performance stats





Capturing Network calls:

To capture the network calls while loading a page, we need to use java script executor, ]the following example may helps:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("disable-info");
    options.addArguments("start-maximized");
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    driver = new ChromeDriver(capabilities);
    driver.get(url);
    //switch to Browser  Functions tab for code implementation
    String netData = ((JavascriptExecutor) driver).executeScript(scriptToExecute).toString();
    BrowserFunctions.waitUntilPageLoads(driver);
    String scriptToExecute = "var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var network = performance.getEntries() || {};
return network;";
    System.out.println(netData);

Note: The out put in json format.



Capture Network performance:

DesiredCapabilities d = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
d.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
WebDriver driver = new ChromeDriver(d);
driver.get("https://www.google.co.in/");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
LogEntries les = driver.manage().logs().get(LogType.PERFORMANCE);
for (LogEntry le : les) {
    System.out.println(le.getMessage());
}





How to import ssl certificate for secure connection


Sometimes it is necessary to import certificates to make secure connections (https://) when it broke. This is like, we download from the broken url (http://) and the we import the same into browser, so called self-signed certificates.

I am using chrome browser here to explain how to import self-signed certificates. After following the images , don't forget to restart the browse



Image - 1
r



Image -2




Note: Restart the browser