vendredi 26 mars 2021

Doing Selenium Test by calling the method with the given parameter to do a test and return the results -java-

I am a newer person to selenium, I work with it using java with the IDE eclipse I did many tests that I find in google, and worked perfectly especially when i move the selenium test to the cloud, you can find a really helpful tutorial for login test here: https://www.lambdatest.com/blog/selenium-java-tutorial-how-to-test-login-process/

so using this tutorial I create this testng class

import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class testauthantification {


    public RemoteWebDriver driver = null;
    public String url = "https://www.lambdatest.com/";
    public static final String  username= "username of lamdatest";
    public static final String auth_key = "zUymhCJ2sIJWHyIsyuV9mXNiwGh5uYwqdmrNoHWVTSW5bN5VG9";
    public static final String URL = "@hub.lambdatest.com/wd/hub";
    boolean status = false;
    private String msg;

    
    

    @Test
    
    public void login () {
        // TODO Auto-generated method stub
        try {

            driver.manage().window().maximize();
            driver.get("https://profile.oracle.com/myprofile/account/secure/update-account.jspx?nexturl=https%3A//www.oracle.com");
     
            WebElement username = driver.findElement(By.id("sso_username"));
            WebElement password = driver.findElement(By.id("ssopassword"));
            WebElement login = driver.findElement(By.id("signin_button"));

            username.sendKeys("*******@gmail.com");
            password.sendKeys("the password");
            login.click();

            String actualUrl = "https://profile.oracle.com/myprofile/account/secure/update-account.jspx?nexturl=https%3A//www.oracle.com";
            String expectedUrl = driver.getCurrentUrl();

            if (actualUrl.equalsIgnoreCase(expectedUrl)) {
                
                 this.msg= "Test passed";
                System.out.println(msg);
                status = true; //Lambda status will be reflected as passed
              } else {
                System.out.println("Test failed"); //Lambda status will be reflected as passed

            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        finally {
            tearDown();
        }


    }
    



    @BeforeClass
    public void setUp() {
        DesiredCapabilities capabilities = new DesiredCapabilities();
    
        capabilities.setCapability("build", "your build name");
        capabilities.setCapability("name", "your test name");
        capabilities.setCapability("platform", "Windows 10");
        capabilities.setCapability("browserName", "Chrome");
        
        capabilities.setCapability("version","89.0");
        try {

            driver = new RemoteWebDriver(new URL("https://" + username + ":" + auth_key + URL), capabilities);

        } catch (Exception e) {

            System.out.println("Invalid grid URL" + e.getMessage());
        }

    }
    private void tearDown () {
        if (driver != null) {
            ((JavascriptExecutor) driver).executeScript("lambda-status=" + status); //Lambda status will be reflected as either passed/ failed

            driver.quit();

            System.out.println("The setup process is completed");
            System.out.println(msg);

        }
    }
    
}

in this example i test the authentification to my oracle account and it worked perfectly and give me this

    [RemoteTestNG] detected TestNG version 7.3.0
    mars 26, 2021 3:02:02 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: W3C
    Test passed
    The setup process is completed
    Test passed
    PASSED: login
    
    ===============================================
        Default test
        Tests run: 1, Failures: 0, Skips: 0
    ===============================================
    
    
    ===============================================

Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

but the problem here is every time I want to test an application I should write a new class again but the difference will just be in the parameter(username password button) that will be ok if you're only testing 4 application or less but what about if you have more than 10 so what I want to ask is about a method that we can create and give it the 3 elements and return us a string msg that the test passed or failed like

string testautantification(Element username, Element password, Element button) for example

(here this class Element contains the IDSELECTOR and its value that we will use in sendkey)

not just that but we can add also like lambdatest parameter and token I hope I was clear in my question if someone can help I am more than greatful.

Aucun commentaire:

Enregistrer un commentaire