Home > Software design >  How to fetch data from website and print in excel Page object model, Data driven and page factory th
How to fetch data from website and print in excel Page object model, Data driven and page factory th

Time:12-13

**Element Page**
public class BasePage {
  @FindBy(className = "_42ft _4jy0 _52e0 _4jy6 _4jy1 selected _51sy")//Signin Button
    private WebElement SigninButton;
    public void clickOnSigninButton() {
        SigninButton.click();
    }

    @FindBy(className= "pam _3-95 _9ay3 uiBoxRed")//Get Username erroe msg
    private WebElement geterrormsg;
    public String ClickonAdmissionnumber() {
        geterrormsg.getText();
    }
}
**Another Class Test case page**
public class Tc_1 {
public void  LoginTC1() throws Exception
{       
**//create page objects**
  BasePage lp = new BasePage();     
**//invoke the methods**
  lp.clickOnSigninButton();
  lp.Errormsg();
**//write actual data to excel**
ExcelLibrary.writeData("Sheet1", 1, 5, Actualmsg);
}    

Trying to fetch text from this class attribute? I have tried with get attribute value but not able to fetch data. Please help me out.... text output "Wrong credentials Invalid username & Password".

enter image description here

CodePudding user response:

Code:

package selenium;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class SaikumarFbTest extends WebDriverSetup {

    public static void main(String[] args) {
        
        // wrapped driver initialization
        WebDriver driver = startChromeDriver();
        
        //entering url
        driver.get("https://www.facebook.com/login");
        
        // accept cookie consent in CZ
        driver.findElement(By.xpath("//button[contains(@data-testid,'cookie-policy-dialog-accept-button')]")).click();  
        
        // switch to english using link in footer
        driver.findElement(By.xpath("//a[contains(@href,'https://www.facebook.com/login')]")).click();
        
        //accept cookie consent in EN
        driver.findElement(By.xpath("//button[contains(@data-testid,'cookie-policy-dialog-accept-button')]")).click();  

        // click login button
        driver.findElement(By.id("loginbutton")).click();
        
        // get <div  id="email_container"></div>
        WebElement emailContainer = driver.findElement(By.id("email_container"));
        
        // get child divs
        List<WebElement> emailContainerChildDivs = emailContainer.findElements(By.tagName("div"));
        
        // print error text
        System.out.println(emailContainerChildDivs.get(1).getText());
        driver.quit();
    }
    
}

Output:

Starting ChromeDriver 96.0.4664.45 (76e4c1bb2ab4671b8beba3444e61c0f17584b2fc-refs/branch-heads/4664@{#947}) on port 23066
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Pro 10, 2021 11:30:39 DOP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
The email or mobile number you entered isn’t connected to an account. Find your account and log in.

To save the text to XLSX file, use Apache POI -> https://www.codejava.net/coding/how-to-write-excel-files-in-java-using-apache-poi.

CodePudding user response:

**Element Page**
public class BasePage {
  @FindBy(className = "_42ft _4jy0 _52e0 _4jy6 _4jy1 selected _51sy")//Signin Button
    private WebElement SigninButton;
    public void clickOnSigninButton() {
        SigninButton.click();
    }

    @FindBy(className= "pam _3-95 _9ay3 uiBoxRed")//Get Username erroe msg
    private WebElement geterrormsg;
    public String ClickonAdmissionnumber() {
       return geterrormsg.getText();
    }
}
**Another Class Test case page**
public class Tc_1 {
public void  LoginTC1() throws Exception
{       
**//create page objects**
  BasePage lp = new BasePage();     
**//invoke the methods**
  lp.clickOnSigninButton();
  String Actualmsg = lp.Errormsg();
**//write actual data to excel**
ExcelLibrary.writeData("Sheet1", 1, 5, Actualmsg);
}    
  • Related