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);

Handling Frames in Selenium:

Selenium has multiple methods to handle Frames.
  1. driver.switchTo().frame(name) 
  2. driver.switchTo().frame(id) 
  3. driver.switchTo().frame(index) 
  4. driver.switchTo().frame(iframe_element) 
Example:
<iframe name=”content” id=”contentSection”></iframe>
Multiple options:

driver.switchTo.frame(“content”);
driver.switchTo.frame(“contentSection”);
driver.switchTo.frame(0);
driver.switchTo.frame(driver.findElement(By.cssSelector("iframe[id='contentSection']")
)


To get out of Frame:
driver.SwitchTo().DefaultContent()
Switch to Parent Frame:
driver.switchTo().parentFrame();