admin管理员组

文章数量:1122832

I have a HTML front-end page with various buttons (different application names) on them. The way it should work is that when a user clicks on one button, it will launch the individual URLs in the Scenario Outline examples and a screenshot is taken of that page and saved to some location. I have completed the Java code and BDD for this load page and screenshot/save function.

The part I need assistance with, is the button click trigger, i.e. when the user clicks on button A or B or C do (above logic).

Because there are multiple buttons on the front-end, how can I write the Java function behind the BDD scenario to check which button the user clicks, so it redirects to the appropriate pages (using examples in scenario outline).

Below is an idea of what I am trying to achieve, but I am open to any alternatives.

@When user clicks on {string} then webdriverAgent opens {url}$  

public boolean checkifbuttonisclicked(String xpath, String url) throws exception {

// if (driver.findElement(By.xpath(xpath)).Click()) {

//}

if (driver.findElement(By.xpath(xpath)).isSelected()) {

    TakeaScreenshotofDesktop(app, yr); //separate function responsible for the screenshot

 }

}

So basically,

Scenario Outline:
Load HTML Page (with multiple buttons)
When user clicks on <button> 
Open <url> corresponding to the app in the example below

Examples
button | url
App1 |  |
App1 |  |
App1 |  |
App2 |  |
App2 |  |

I have a HTML front-end page with various buttons (different application names) on them. The way it should work is that when a user clicks on one button, it will launch the individual URLs in the Scenario Outline examples and a screenshot is taken of that page and saved to some location. I have completed the Java code and BDD for this load page and screenshot/save function.

The part I need assistance with, is the button click trigger, i.e. when the user clicks on button A or B or C do (above logic).

Because there are multiple buttons on the front-end, how can I write the Java function behind the BDD scenario to check which button the user clicks, so it redirects to the appropriate pages (using examples in scenario outline).

Below is an idea of what I am trying to achieve, but I am open to any alternatives.

@When user clicks on {string} then webdriverAgent opens {url}$  

public boolean checkifbuttonisclicked(String xpath, String url) throws exception {

// if (driver.findElement(By.xpath(xpath)).Click()) {

//}

if (driver.findElement(By.xpath(xpath)).isSelected()) {

    TakeaScreenshotofDesktop(app, yr); //separate function responsible for the screenshot

 }

}

So basically,

Scenario Outline:
Load HTML Page (with multiple buttons)
When user clicks on <button> 
Open <url> corresponding to the app in the example below

Examples
button | url
App1 | https://stackoverflow.com |
App1 | https://reddit.com |
App1 | https://timer.com |
App2 | https://google.com |
App2 | https://fenty.com |
Share Improve this question edited yesterday guud.purson asked Dec 27, 2024 at 22:40 guud.pursonguud.purson 113 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

It seems like you're making this more complicated than it needs to be.

On the HTML front end page, create an INPUT for each desired application/URL, e.g.

<form action="https://stackoverflow.com">
    <input type="submit" value="App1" />
</form>
<form action="https://google.com">
    <input type="submit" value="App2" />
</form>

That way when you click on the button, it navigates to the desired URL.

Now for the Java method. You pass in the App name, e.g. "App1", and click on the corresponding INPUT with that value.

public void clickApp(String appName) {
    driver.findElement(By.cssSelector("input[value='" + appName + "']")).click();
}

When you do it this way, you don't have to pass in the URL because it's in the HTML of the page. You also don't need to check for .isSelected(), etc., just click on the corresponding button.

本文标签: