Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
New to Selenium and working on few hands on activity. Below is my query:

An application has been developed which enrolls the user details to the shipment company. Using the application user can track their shipment status and details.

URL: http://webapps.tekstac.com/Handling_Regular_Expression/[^]

Below is the Test Procedure:

1.Use the template code.

2.Don't make any changes in DriverSetup file.

3.Only in the suggested section add the code to,

4.Invoke the driver using getWebDriver() method defined in DriverSetup()

Implement the following methods:

public void setFormValues(WebDriver driver)

5.Identify the shipment for user text box and set the value as “Shamili”

6.Identify the Search button and click on the same

public WebElement getNameResultElement(WebDriver driver)

7.In the result page, find the element of 'Shamili' and return it. public WebElement getShipmentResultElement(WebDriver driver)

8.In the result page, find the element of 'SHIP1236' and return it. public WebElement getEmailResultElement(WebDriver driver)

9.In the result page, find the element of 'shamili93@gamil.com' and return it.

10.Get the e-mail from div id "e- mail" and shipment id from div id "shipment".

11.Validate the values of e-mail and shipmentId using the below methods.

public boolean validateEmail(String eMailID) { public boolean validateShipmentId(String shipmentId)

Validation pattern is given below,

mail:\\b[A-Z0-9a-z-]+@[a-z]+\\.[a-z]{2,4}\\b
ShipmentID:[A-Z0-9]{8}


12.Return 'true' or 'false' boolean value based on the findings.

What I have tried:

This is the code that I have done so far. Saw few regEx examples online but none seems to work when I compile it and I am stuck.

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

public class NameLocator {      //DO NOT change the class name
 
    public static String baseUrl; //Assign 'http://webapps.tekstac.com/Handling_Regular_Expression/' for baseUrl
    public static WebDriver driver;
    
    public WebDriver createDriver()
    {
         DriverSetup ds = new DriverSetup();
         driver =  ds.getWebDriver();
         baseUrl = "http://webapps.tekstac.com/Handling_Regular_Expression/";
         return driver;
        //Create driver. Assign it to static variable 'driver' and return it
    }
    
    public void navigate(WebDriver driver){
        
         driver.navigate().to(baseUrl);
         //Navigate to the baseUrl
    }
    
    public void setFormValues(WebDriver driver)
    {
        WebElement userId = driver.findElement(By.id("userId"));
        userId.sendKeys("Shamili");
        WebElement track = driver.findElement(By.id("track"));
        track.click();
        //set the value for 'Shipment for user' and submit form
    }

    public WebElement getNameResultElement(WebDriver driver) {
        return driver.findElement(By.xpath("/html/body/div/table/tbody/tr[1]/td[2"));
        //Find the element of 'Shamili' and return it
        
    }
    public WebElement getShipmentResultElement(WebDriver driver) {
        return driver.findElement(By.id("shipment"));
         //Find the element of 'SHIP1236' and return it
    }
    public WebElement getEmailResultElement(WebDriver driver) {
        return driver.findElement(By.id("e- mail"));
        //Find the element of 'shamili93@gamil.com' and return it
    }
    
    public boolean validateEmail(String eMailID) {
       String Emailregex = "^[A-Za-z]\\w{5,29}$";
       
       //Validate email using regex. 
        
    }
    public boolean validateShipmentId(String shipmentId) {
       String Shipmentregex = "[A-Z0-9]{8}";
       
        //Validate shipmentId using regex
        
    }    
        
        
       
  
    public static void main(String[] args)
    {
        NameLocator reg=new NameLocator();
         //Add required code here
    }

  
}
Posted
Updated 18-Jul-22 9:05am
v3
Comments
Richard MacCutchan 13-Jul-22 8:33am    
What is the question?
SherinFemina 13-Jul-22 9:59am    
I need to validate the values and return 'true' or 'false' boolean value using RegEx
Richard MacCutchan 13-Jul-22 10:20am    
It is fairly obvious from the title of your question that that is what you want to do. But you still need to explain what the actual problem is with the code you have written.
SherinFemina 18-Jul-22 4:02am    
While compiling my code, I am getting error in 2 places - Validate emailID and Validate ShapmentID. Not sure what should be done to fix it. The errors were - Missing return statement.
Richard MacCutchan 18-Jul-22 4:27am    
What error?

1 solution

Not sure i understand you well, but...

If you want to return boolean value, use find() method.
See:
java - regex isMatch returns false - Stack Overflow[^]
Difference between matches() and find() in Java Regex - Stack Overflow[^]

Java
public static void main(String[] args) throws ParseException {
    Pattern p = Pattern.compile("\\d\\d\\d");
    Matcher m = p.matcher("a123b");
    System.out.println(m.find());
    System.out.println(m.matches());

    p = Pattern.compile("^\\d\\d\\d$");
    m = p.matcher("123");
    System.out.println(m.find());
    System.out.println(m.matches());
}

/* output:
true
false
true
true
*/


More:
Java - Regular Expressions[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900