Fillo Implementation - java

I came across a really nice and simple xls and xlsx implementation API called Fillo. I've made a spread sheet with the following values as most people know the all caps sections are for category names to seac
USERID PASS
joe bigjoe
jim bigjim
john bigjohn
Now here's the code:
import Exception.FilloException;
import Fillo.*;
public class CallBack {
public static void main(String args[]) throws FilloException {
testFillo();
}
private static void testFillo() throws FilloException {
Fillo fillo=new Fillo();
Connection connection=fillo.getConnection("logindatabase.xls");
String strQuery="Select * from Sheet1 where USERID='*' and PASS='*'";
Recordset recordset=connection.executeQuery(strQuery);
/*
* I'm wanting the login check to be right here
*/
recordset.close();
connection.close();
}
}
The login function should go through the USERID column first and once it finds that it will check the corresponding PASS value. So if it found "jim" as the user it then check for "bigjim" as the password.
So the big question would be: How would I set something like this up?
Here's the Fillo Documentation.
All the best,
Groax

you can try this... It's not completely customized but it will help...
public static void selectData(String tcid,String fieldName) throws FilloException{
Fillo fillo=new Fillo();
Connection connection=fillo.getConnection("testdata//testcasedata.xlsx");
String strQuery="Select * from data where TCID='"+tcid+"'";
Recordset recordset=connection.executeQuery(strQuery);
while(recordset.next()){
ArrayList<String> dataColl=recordset.getFieldNames();
//System.out.println(dataColl);
Iterator<String> dataIterator=dataColl.iterator();
//System.out.println(dataColl.size());
while(dataIterator.hasNext()){
for (int i=0;i<=dataColl.size()-1;i++){
//System.out.println(i);
String data=dataIterator.next();
String dataVal=recordset.getField(data);
if (dataVal.equalsIgnoreCase(fieldName)){
//System.out.println("passed");
i=i+1;
//System.out.println(i);
String testData=dataColl.get(i);
System.out.println(recordset.getField(testData));
}
}
break;
}
}
recordset.close();
connection.close();
}

You will have to arrange your table like database as fillo works like database.
Put everything related to a test case in a row. as attached.
below is the code for reader and calling the login class.
package com.evs.vtiger.framework.util;
import java.util.ArrayList;
import java.util.Iterator;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import com.evs.vtiger.framework.pages.home.myhomepage.HomePage;
import Exception.FilloException;
import Fillo.Connection;
import Fillo.Fillo;
import Fillo.Recordset;
import com.evs.vtiger.framework.pages.login.login.LoginPage;
public class TestingFillo {
public static void main(String[] args) throws FilloException {
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://localhost:8080/NBOWeb");
LoginPage lpObj=PageFactory.initElements(driver, LoginPage.class);
lpObj.pg_ValidLogin("TC001");
}
public static String getTestValue(String TCID, String fieldName) throws FilloException{
String testString=xlTesting(TCID, fieldName);
return testString;
}
public static String xlTesting(String tcid,String fieldName) throws FilloException{
String testval=null;
Fillo fillo=new Fillo();
Connection connection=fillo.getConnection("TestData//TestData.xlsx");
String strQuery="Select * from data where TCID='"+tcid+"'";
Recordset recordset=connection.executeQuery(strQuery);
while(recordset.next()){
ArrayList<String> dataColl=recordset.getFieldNames();
//System.out.println(dataColl);
Iterator<String> dataIterator=dataColl.iterator();
//System.out.println(dataColl.size());
while(dataIterator.hasNext()){
for (int i=0;i<=dataColl.size()-1;i++){
//System.out.println(i);
String data=dataIterator.next();
String dataVal=recordset.getField(data);
if (dataVal.equalsIgnoreCase(fieldName)){
//System.out.println("passed");
i=i+1;
//System.out.println(i);
String testData=dataColl.get(i);
//System.out.println(recordset.getField(testData));
String testValue= recordset.getField(testData);
testval=testValue;
}
}
break;
}
}
recordset.close();
connection.close();
return testval;
}
public static void inputText(WebElement we, String fieldName, String TCID) throws FilloException{
String fval=getTestValue(TCID, fieldName);
we.sendKeys(fval);
}
}
Now here is the code of login
package com.evs.vtiger.framework.pages.login.login;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import Exception.FilloException;
import com.evs.vtiger.framework.pages.home.myhomepage.HomePage;
import com.evs.vtiger.framework.util.TestingFillo;
import com.evs.vtiger.framework.util.UI;
import com.evs.vtiger.framework.util.XLReader;
public class LoginPage {
#FindBy(name="username")
public WebElement UserName_ED;
#FindBy(xpath="//input[#type='password']")
public WebElement Password_ED;
#FindBy(xpath="//input[#value='login']")
public WebElement Login_BT;
public void pg_ValidLogin(String TCID) throws FilloException {
/*UI.fn_Input(UserName_ED, "UserName_ED");
UI.fn_Input(Password_ED, "Password_ED");
UI.fn_Click(Login_BT);*/
//UserName_ED.sendKeys("abc");
TestingFillo.inputText(UserName_ED, "UserName_ED", TCID);
TestingFillo.inputText(Password_ED, "Password_ED", TCID);
Login_BT.click();
// HomePage hpObj=PageFactory.initElements(UI.driver, HomePage.class);
// return hpObj;
}
public void pg_InValidLogin() {
UserName_ED.sendKeys("rahul");
Password_ED.sendKeys("admin");
Login_BT.click();
}
}
I hope it makes sense. Please ignore the commented code. sorry for that.
Thanks
Nitin

Related

driver.get is ignored on second data set of Testng test

The code in the "basePageNavigation" #Test is running fine on the first data set, but when the method is ran the second time, the first line in it:
driver.get(prop.getProperty("url"));
is ignored, and thus the test fails on the first findElement method called. When I run this in debug mode having a breakpoint at that line, it works fine. Code:
package Academy;
import java.io.IOException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import pageObjects.ForgotPassword;
import pageObjects.Landingpage;
import pageObjects.Loginpage;
import resources.base;
public class Homepage extends base{
public static Logger log = LogManager.getLogger(Homepage.class.getName());
public WebDriver driver;
#BeforeTest
public void initialize() throws IOException
{
driver=initializeDriver();
log.info("Driver initialized");
}
#Test(dataProvider="getData")
public void basePageNavigation(String username, String password, String text) throws IOException
{
driver.get(prop.getProperty("url"));
Landingpage l=new Landingpage(driver);
Loginpage lp = l.getLogin();
lp.getEmail().sendKeys(username);
lp.getPassword().sendKeys(password);
lp.getLoginBtn().click();
ForgotPassword fp = lp.forgotPassword();
fp.getEmail().sendKeys("test");
fp.nextButton().click();
}
#DataProvider
public Object[][] getData()
{
//Row (first value) stands for how many different data types the test will run
//Column (second value) stands for how many values are parsed per test
Object[][] data = new Object[2][3];
// First row
data[0][0] = "nonResabc#qa.com";
data[0][1] = "12345";
data[0][2] = "Non Restricted User";
//Second Row
data[1][0] = "Resabc#qa.com";
data[1][1] = "34567";
data[1][2] = "Restricted User";
return data;
}
#AfterTest
public void tearDown()
{
driver.close();
}
}

java.lang.NullPointerException error on Page Object Model with cucumber

I am getting java.lang.NullPointerException while following Page Object Model with Cucumber. I am not sure what I am doing wrong here, please help me on this
Below is my Test Base Class:
package com.qa.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class TestBase {
public static WebDriver driver;
public static Properties prop;
//public WebDriver initializeWebDriver() throws IOException
public static void initializeWebDriver() throws IOException
{
prop = new Properties();
FileInputStream fis = new FileInputStream("D:\\Automation\\WebAutomation\\src\\main\\java\\com\\qa\\config\\config.properties");
prop.load(fis);
String browserName = prop.getProperty("browser");
//Execute in Chrome
if(browserName.equals("Chrome"))
{
System.setProperty("webdriver.chrome.driver","D:\\Drivers\\chromedriver.exe");
driver=new ChromeDriver();
//driver.manage().window().maximize();
}
//Execute in FireFox
else if(browserName.equals("Firefox"))
{
System.setProperty("webdriver.gecko.driver","D:\\Drivers\\geckodriver-v0.19.1-win64(1)");
driver = new FirefoxDriver();
}
driver.manage().window().maximize();
driver.get(prop.getProperty("appURL"));
driver.manage().timeouts().pageLoadTimeout(TestUtil.PAGE_LOAD_TIMEOUT,TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(TestUtil.IMPLICIT_WAIT, TimeUnit.SECONDS);
return driver;
}
}
Below is my login page Objects Class
package com.qa.pages;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.qa.util.TestBase;
public class LoginPage extends TestBase {
public LoginPage() {
/*super(driver);
this.driver=driver;*/
PageFactory.initElements(driver, this);
}
// Login Page Title
public String validateLoginPageTitle() {
return driver.getTitle();
}
// Welcome text
#FindBy(css=".login-form > h2:nth-child(1)")
WebElement header;
public String loginPageHeaderText() {
return header.getText();
}
}
Below is my Step Def
package com.qa.stepdefinations;
import java.io.IOException;
import org.testng.Assert;
import com.qa.pages.LoginPage;
import com.qa.util.TestBase;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class LoginStepDef extends TestBase {
LoginPage LoginPage = new LoginPage();
#Given("^I launch browser and access the GE URL$")
public void i_launch_browser() throws IOException {
TestBase.initializeWebDriver();
}
#Then("^I am on Login Page$")
public void i_am_on_login_page() {
String expectedLoginPageTile = prop.getProperty("LoginPage_Title");
String actualLoginPageTitle = LoginPage.validateLoginPageTitle();
Assert.assertEquals(actualLoginPageTitle, expectedLoginPageTile);
}
#Then("^I verify header text is displaying$")
public void i_verify_header_text_is_displaying() {
String expectedHeaderText = prop.getProperty("LoginPage_Expected_Header");
String actualdHeaderText = LoginPage.loginPageHeaderText();
Assert.assertEquals(actualdHeaderText, expectedHeaderText);
}
}
The script is working fine for LoginPage.validateLoginPageTitle(); however, I am not sure why it is not working for the next step i.e. LoginPage.loginPageHeaderText();
It seems issue is with your locator, check if it is correct.
#FindBy(css="<b><em>.login-form > h2:nth-child(1)</em></b>")
WebElement header;

Missing return statement, how can I fix?

Goal: I have a class called "InvokeChromeTest" I'm extending to another class called "Base" to access chromedriver and data.properties. When running my code, I get the error:
Error:(22, 3) java: missing return statement
I am unsure how to fix this. Here is my sample code as follows. Please let me know what I can do to fix.
src/test/java/loginPage/InvokeChromeTest
package loginPage;
import credentials.ProfileCredentials;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
import resources.Base;
public class InvokeChromeTest extends Base {
#Test
public WebDriver initializeDriver() {
driver = initializeDriver();
driver.get(dataProperties.getProperty("url"));
ProfileCredentials p = new ProfileCredentials(driver);
p.getAuthorize().click();
p.getApiKey().sendKeys("testKey");
p.getAuthCred().click();
p.getCloseAuth().click();
}
#AfterTest
public void teardown() {
driver.close();
}
}
src/main/java/resources/Base
package resources;
import io.github.bonigarcia.wdm.WebDriverManager;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Base {
public WebDriver driver;
protected Properties dataProperties;
public WebDriver initializeDriver() {
// Create global property file
dataProperties = new Properties();
InputStream dataPropertiesInputStream = null;
try{
dataPropertiesInputStream = getClass().getClassLoader().getResourceAsStream("data.properties");
dataProperties.load(dataPropertiesInputStream);
} catch (IOException e) {
e.printStackTrace();
}
String browserName = dataProperties.getProperty("browser");
System.out.println(browserName);
if (browserName.equals("chrome")) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
} else if (browserName.equals("firefox")) {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
return driver;
}
}
src/main/java/credentials/ProfileCredentials
package credentials;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class ProfileCredentials {
WebDriver driver;
public ProfileCredentials(WebDriver driver) {
this.driver = driver;
}
private By authorize = By.xpath("//button[#class='btn authorize unlocked']");
private By apikey = By.xpath("//div[#class='wrapper']//section//input");
private By authorizecred = By.xpath("//button[#class='btn modal-btn auth authorize button']");
private By closeauth = By.xpath("//button[#class='btn modal-btn auth btn-done button']");
public WebElement getAuthorize() {
return driver.findElement(authorize);
}
public WebElement getApiKey() {
return driver.findElement(apikey);
}
public WebElement getAuthCred() {
return driver.findElement(authorizecred);
}
public WebElement getCloseAuth() {
return driver.findElement(closeauth);
}
}
I figured out the problem. "Base" and "InvokeChromeTest" had the same method name. This is why it was recursive.

Selenium + JUnit, Assertion error of proper page loaded

I've written a code using Selenium WD and JUnit that should check if proper page is loaded and the table of this page is not null
Besides assertion, to make sure that proper page is loaded (checking if URL contains specified text), I've put "if-else" statement in "waitUntilPageLoaded" method.
import junit.framework.TestCase;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.junit.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class GuruSearch
{
#Test
static void checkDemoLogin()
{
System.setProperty("webdriver.gecko.driver", "C:\\Users\\pansa\\Documents\\Webdrivers\\geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver();
driver.get("https://demo.guru99.com/");
TestCase.assertFalse(driver.getPageSource().contains("404"));
driver.manage().window().maximize();
//odczekaj(5000);
WebElement browser = driver.findElement(By.name("emailid"));
browser.sendKeys("Test#test.com");
browser.submit();
waitUntilPageLoaded(driver,"access.php", 10);
TestCase.assertTrue(driver.getPageSource().contains("Access details to demo site"));
WebElement table = driver.findElement(By.tagName("tbody"));
TestCase.assertNotNull(table);
driver.close();
}
static private void odczekaj(int czas)
{
try {
Thread.sleep(czas);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("Przerwanie");
}
}
private static void waitUntilPageLoaded(FirefoxDriver d, String zawiera, int timeout)
{
WebDriverWait wait = new WebDriverWait(d, timeout);
wait.until(ExpectedConditions.urlContains(zawiera));
if (d.getCurrentUrl().contains(zawiera))
{
System.out.println("OK");
}
else
{
System.out.println("NOK");
}
}
}
The "if-else" statement in "waitUntilPageLoaded" method returns "OK", but TestCase.assertTrue(driver.getPageSource().contains("Access details to demo site"))
throws AssertionError, although the text appears in the page.
Why there is AssertionError thrown?
As you are using the JUnit annotations, the annotated method shouldn't be static. I have modified your code a bit, try the below:
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import junit.framework.TestCase;
public class GuruSearch
{
#Test
public void checkDemoLogin()
{
System.setProperty("webdriver.chrome.driver", "C:\\NotBackedUp\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://demo.guru99.com/");
TestCase.assertFalse(driver.getPageSource().contains("404"));
driver.manage().window().maximize();
//odczekaj(5000);
WebElement browser = driver.findElement(By.name("emailid"));
browser.sendKeys("Test#test.com");
browser.submit();
waitUntilPageLoaded(driver,"access.php", 30);
TestCase.assertTrue(driver.getPageSource().contains("Access details to demo site"));
WebElement table = driver.findElement(By.tagName("tbody"));
TestCase.assertNotNull(table);
driver.close();
}
static private void odczekaj(int czas)
{
try {
Thread.sleep(czas);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("Przerwanie");
}
}
private static void waitUntilPageLoaded(WebDriver d, String zawiera, int timeout)
{
WebDriverWait wait = new WebDriverWait(d, timeout);
wait.until(ExpectedConditions.urlContains(zawiera));
if (d.getCurrentUrl().contains(zawiera))
{
System.out.println("OK");
}
else
{
System.out.println("NOK");
}
}
}
The above code is printing 'OK' and Assert condition also passing. And I think, it doesn't matter if we give upper/lower case in the contains. It will match both the cases.
I don't have Firefox in my system so I have checked with Chrome, you change the browser and try... I hope it helps...

Cucumber Framework setup

I was getting the following error message
The attribute value is undefined for the annotation type Given.
The import cucumber.annotation cannot be resolved
See code:
package annotation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;
public class annotation {
WebDriver driver = null;
#Given("^I am on Facebook login page$")
public void goToFacebook() {
driver = new FirefoxDriver();
driver.navigate().to("https://www.facebook.com/");
}
#When("^I enter username as \"(.*)\"$")
public void enterUsername(String arg1) {
driver.findElement(By.id("email")).sendKeys(arg1);
}
#When ("^I enter password as \"(.*)\"$")
public void enterPassword(String arg1) {
driver.findElement(By.id("pass")).sendKeys(arg1);
driver.findElement(By.id("u_0_v")).click();
}
#Then("^Login should fail$")
public void checkFail() {
if(driver.getCurrentUrl().equalsIgnoreCase(
"https://www.facebook.com/login.php?login_attempt=1&lwv=110")) {
System.out.println("Test1 Pass");
} else {
System.out.println("Test1 Failed");
}
driver.close();
}
#Then("^Relogin option should be available$")
public void checkRelogin() {
if(driver.getCurrentUrl().equalsIgnoreCase(
"https://www.facebook.com/login.php?login_attempt=1&lwv=110")){
System.out.println("Test2 Pass");
} else {
System.out.println("Test2 Failed");
}
driver.close();
}
}
Use these import statements
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
Also not a great idea to name a class 'annotation' or a package the same and also use the naming standards. Like classes should begin with capitals.
http://www.oracle.com/technetwork/java/codeconventions-135099.html

Categories