Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Sunday, May 10, 2020

TestNG Assertion in Selenium WebDriver with Java

Test Scenario/Steps:

1. Open a browser (in my case Firefox)
2. Navigate to test URL say https://link.testproject.io/bsg
3. Assert the title of page
4. Click Login link at the top right corner
5. Enter invalid Username & Email --> Click Sign in button
6. Login will fail and a notification message will be displayed --> Assert the validation message

The following is the complete Selenium java code using TestNG framework:

package assertion_verification;

import java.util.ArrayList;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

public class ValidationOfInvalidLogin_TestNG {
private WebDriver driver;
JavascriptExecutor js;
String baseURL;
@BeforeClass
public void setUp() {
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "\\" + "src\\test\\resources\\Driver\\geckodriver-v0.24.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();
js = (JavascriptExecutor) driver;
baseURL = "https://link.testproject.io/bsg";
}
@AfterClass
public void tearDown() {
driver.quit();
}

@Test
public void validationOfInvalidLogin() throws Exception{
//Open test URL: https://link.testproject.io/bsg
driver.get(baseURL);
Thread.sleep(3000);
//assertion of title of page using TestNG
String actualTitle = driver.getTitle();
String expectedTitle = "Free Test Automation For All – TestProject";
assertEquals(expectedTitle,actualTitle);
//OR,
assertTrue(actualTitle.contains(expectedTitle));

String parentWindowHandle = driver.getWindowHandle();
driver.findElement(By.linkText("Login")).click();
Thread.sleep(3000);

ArrayList<String> windowHandles = new ArrayList<String>(driver.getWindowHandles());
for (String window:windowHandles){
if (window != parentWindowHandle){
driver.switchTo().window(window);
}
}
Thread.sleep(3000);

driver.findElement(By.name("username")).clear();
driver.findElement(By.name("username")).sendKeys("fff@gmail.com");

driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys("1234567890");
//Click Signin button
driver.findElement(By.id("tp-sign-in")).click();
//Assertion of notification message with TestNG
String actualMsg = driver.findElement(By.id("tp-message-error")).getText();
String expectedMsg = "Invalid username or password.";
assertEquals(expectedMsg,actualMsg);
}
}

Wednesday, March 4, 2020

Convert/Export Recorded Web, Android & iOS Tests to Java & C# with TestProject


Prerequisites:
1. Sign up: Open a browser  -> Visit TestProject and sign up
2. Activate your TestProject A/C: After signup go to your email and activate account by clicking 
    the link provided through your email
3. Login: Go to TestProject -> Click Login link at the top right corner and log in
4. Get TestProject Agent: Mouse over Agents menu item and download agent for your preferred
    OS (Windows, Linux, Mac)
5. Install, Launch and Register Agent: Install & launch Agent -> Register it
6. Record test: Record your desired tests by following the instructions from the link below:

The following is my recorded tests of Google search:

Now is the time to export your Recorded Tests to Code (Java or .NET C#) ...

Export recorded test to Java or .NET C#:

1. In order to export recorded tests to code, 1st step is to click the Three Dots at the right corner of
    your test and choose to “Generated Code” for the test you want to export as the screenshot as
    below:

2. The following option will be displayed:

3. Select Java or C# as programming language and select your preferred browser (such as Chrome,
    Edge, Firefox, IE, Safari) -> Click Generate button as shown in the screenshot above.
4. Save As option will be appeared for saving the exported code as below screenshot:

5. Now select your desired location -> Click Save button as above screenshot. Click OK from
    Generate code pop up.
6. Extract the zip file you saved (Say, Web_Test_2-My_Test_2-2020_03_02_12_25.zip).
7. Open the extracted code in your preferred IDE. Such as
    for Java you can use Eclipse, IntelliJIDEA.
    for .Net C# you can use Microsoft Visual Studio.


So, You can try TestProject for easier recording your tests as well as to convert your preferred programming language for making flexible & customizable.

Sunday, January 12, 2020

How to install Java/JDK on Windows 10

To install Java on Windows 10:

1. Go to https://www.oracle.com/technetwork/java/javase/downloads/ and download Java SE Development Kit (in my case "jdk-13.0.1_windows-x64_bin.exe")
2. Simply install the exe

Configuration: It needs to set Path and Classpath
1. Go to System window (Control Panel -> System and Security -> System)
2. Click "Advanced system settings" to go to System Properties window
3. Click "Environment Variables"
4. In <System variables> set the followings as below (The value depends on your installation folder/path):
          JAVA_HOME = C:\Program Files\Java\jdk-13.0.1
          Path = C:\Program Files\Java\jdk-13.0.1\bin
          CLASSPATH = .;C:\Program Files\Java\jdk-13.0.1
5. Open command prompt -> Type the following command and press Enter
java -version

Wednesday, November 6, 2019

Test Automation for Windows Desktop Application using Winium with Java

Prerequisites to work with Winium:

1. Microsoft .NET Framework
2. Eclipse (as IDE)
3. Java/JDK
4. TestNG
5. Maven
6. Inspection Element tool (such as Inspect.exe)

Step 1: Creating a Maven project

Open Eclipse IDE and create a Maven project. Update the pom.xml with the necessary dependencies. Dependencies to include – Winium WebDriver, Winium Elements Desktop, Selenium Java, TestNG.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>WiniumWithJava</groupId>
  <artifactId>WiniumWithJava</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <dependencies>
  <!-- https://mvnrepository.com/artifact/com.github.2gis.winium/winium-webdriver -->
<dependency>
    <groupId>com.github.2gis.winium</groupId>
    <artifactId>winium-webdriver</artifactId>
    <version>0.1.0-1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.2gis.winium/winium-elements-desktop -->
<dependency>
    <groupId>com.github.2gis.winium</groupId>
    <artifactId>winium-elements-desktop</artifactId>
    <version>0.1.0-1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.0.0</version>
    <scope>test</scope>
</dependency>
  </dependencies>
</project>

Step 2: Downloading Winium.Desktop.Driver.exe

Download the latest Winium.Desktop.Driver.zip from below link -> extract and keep in any location.
https://github.com/2gis/Winium.Desktop/releases

Step 3: Writing Winium Java code

import java.io.File;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.winium.DesktopOptions;
import org.openqa.selenium.winium.WiniumDriver;
import org.openqa.selenium.winium.WiniumDriverService;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class NotepadExample{
    WiniumDriver driver;
    DesktopOptions option;
    WiniumDriverService service;

@BeforeClass(alwaysRun = true)
public void setUp() throws Exception {
option = new DesktopOptions();
option.setApplicationPath("C:\\Windows\\System32\\notepad.exe");
File driverPath = new File("E:\\QA_720\\Winium\\Winium.Desktop.Driver\\Winium.Desktop.Driver.exe");
service = new WiniumDriverService.Builder().usingDriverExecutable(driverPath).usingPort(9999).withVerbose(true).withSilent(false).buildDesktopService();
try{
service.start();
}catch(IOException ioe){
System.out.println("Exception while starting WINIUM service");
ioe.printStackTrace();
}
driver = new WiniumDriver(service, option);
}
@AfterClass(alwaysRun = true)
public void tearDown() throws Exception {
service.stop();
}

@Test
public void testNotepad() throws Exception{
Thread.sleep(1000);
driver.findElement(By.name("Text Editor")).sendKeys("This is sample Winium test");
driver.findElement(By.id("Close")).click();
driver.findElement(By.name("Don't Save")).click();
}
}

Step 4: Running the test using TestNG

In Eclipse project, right click on the class "NotepadExample.java" -> Mouse over Run As -> Click TestNG Test

Enjoy test execution of Notepad. The following steps would do by running this:
1. Open Notepad
2. Writing some text on Notepad
3. Close the Notepad without saving it

Thursday, November 9, 2017

How to convert Selenium IDE tests into Java and other languages

By default, Selenium IDE tests are recorded as HTML format. This HTML test script can be converted to Selenium WebDriver test script in various supported programming languages such as Java, C#, Ruby, Python, Perl, PHP etc.

There are 2 ways to convert a recorded tests in Selenium IDE into Java code:

1st way: To see the Java code directly in Selenium IDE

1. Open Selenium IDE -> Record a test
2. Go to Options menu -> Options...
3. At "Selenium IDE Options" window: Check the check box for "Enable experimental features" and click OK button
4. Options -> Format -> Java / TestNG / WebDriver
5. Click OK button from "JavaScript Application" pop up -> You will see the desired Java source code for your recorded tests

2nd way: By exporting recorded test case(HTML format) into java file:

1. Record your tests in HTML format by using Selenium IDE
2. File menu -> Export Test Case As -> Java / TestNG / WebDriver
3. Save the file (it would be saved as .java)
4. Open the java file you saved in any text editor or IDE such as, Notepad++, Notepad, Wordpad, Eclipse, IntelliJ IDEA etc. (You can see the expected Java Selenium source code)

There is another way to convert Selenium IDE command individually to any language. The following steps for java:

1. Open Firefox -> Open Selenium IDE
2. Record some steps of your test
3. In Selenium IDE: Go to Options | Clipboard Format and select Java / TestNG / WebDriver
4. Right click any command in Selenium IDE you recorded -> Click Copy
5. Paste in any editor (such as NotePad, Wordpad etc. OR any IDE you are using such as eclipse, IntelliJ IDEA)
6. The command will be pasted as Java format

Note: In similar way you can convert individual Selenium IDE command to C#, Python and many more you want.