Headless execution of test cases using selenium on Firefox with java

package com.sample;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import java.util.concurrent.TimeUnit;
public class HeadlessFirefoxSeleniumSample {
public static void main(String [] args) {
FirefoxBinary firefoxBinary = new FirefoxBinary();
firefoxBinary.addCommandLineOptions("--headless");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setBinary(firefoxBinary);
FirefoxDriver driver = new FirefoxDriver(firefoxOptions);
driver.get("http://virtualbuffer.blogspot.com/");
driver.findelement(By.linkText("Log4Net implementation in Visual Studio")).click();
driver.quit();
}
}

Log4Net implementation in Visual Studio



1.       Right click on Solution and select ‘Manage NuGet Packages for Solution…’.
2.       Click on Browse, search ‘log4net’ and install Log4net.
3.       Add [assembly: log4net.Config.XmlConfigurator(Watch = true)] after [assembly: AssemblyCulture("")] line in ‘AssemblyInfo.cs’ under Properties.
4.       Add  private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); to the class where you want to add logging.
5.       Add below line to App.config under “Configuration” tag.
<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="MyAppender" />
      <appender-ref ref="MyFileAppender" />
    </root>
    <appender name="MyAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %level %logger - %message%newline" />
      </layout>
    </appender>
    <appender name="MyFileAppender" type="log4net.Appender.FileAppender">
      <file type="log4net.Util.PatternString" value="C:\\TestLog.log" />
      <appendToFile value="true" />
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %level %logger - %message%newline" />
      </layout>
    </appender>
  </log4net>
  <startup>


Enjoy logging……….

CodedUI Run Application with Admin User

System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo(@"C:\Program Files (x86)\Notepad++\notepad++.exe");
            proc.UseShellExecute = true;
            proc.Verb = "runas";

Exception in thread "main" java.lang.IllegalArgumentException: Cannot locate declared field class org.apache.http.impl.client.HttpClientBuilder.dnsResolver



This error occurs when you are using old version of http client jar.



You can use below dependency for maven:

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>

Handle windows with Selenium:

 There are test cases in which we need to switch between multiple windows, to automate these test cases using selenium we need to use below functions:
1.getWindowHandle(): This function return parent window reference.
2.getWindowHandles() : This function return all windows reference.
3.switchTo().window(Window) : This function used to switch between windows.
Example:
String Parent_Window = driver.getWindowHandle();
for (String Child_Window : driver.getWindowHandles())
{
driver.switchTo().window(Child_Window);
driver.findElement(By.id("appointment")).click();
driver.findElement(By.id("closeAppointment")).click();
driver.close();
}

driver.switchTo().window(Parent_Window);