-1

I have a button with the follow html:

<span ng-show="!IsGoogleLinked()" aria-hidden="false" class="">Link Google</span>
<span ng-show="IsGoogleLinked()" aria-hidden="true" class="ng-hide">UnLink Google</span>

When you click this button and it goes through the linking process the button changes text from "Link Google" to "Unlink Google". I want to use the webdriver in such a way that'll check if the text in the button is "Unlink Google" it will click the button, and I'm missing a piece I hope someone could provide.

potatocode
  • 13
  • 1
  • 6
  • 1
    Hello! What have you tried so far? I would probably start with a selector to get the element in either state, and then call `getText()` on that element for your if statement – mrfreester Mar 21 '17 at 18:17
  • @mrfreester updated with an answer, it wasn't text I was after, it was elements that aren't currently being displayed that I needed. – potatocode Mar 23 '17 at 21:09

1 Answers1

0

Here is the updated version using individual browser selection:

@When("^I check if the user has a \"([^\"]*)\" linked account$")
public void linkChecker(String socialLoginXpathNotLinked) throws Throwable {
    driverWait = new WebDriverWait(driver, 20);
    driverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(socialLoginXpathNotLinked)));
    driverElement = driver.findElement(By.xpath(socialLoginXpathNotLinked));
    if(!driverElement.isDisplayed())
    {       
        isLinked = true;
    }   
}
@Then("^I unlink user if \"([^\"]*)\" linked$")
public void unlinkUser(String socialLoginXpathLinked) throws Throwable {
    if(isLinked)
    {           
       driver.findElement(By.xpath(socialLoginXpathLinked)).click();
       isLinked = false;
    }
}

And where I call my browser initialization which is what JeffC recommended:

@Given("^I use \"([^\"]*)\" browser$")
public void browserInitialization(String browser) throws Throwable {
if (browser.equals("Chrome"))
{
    System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
    driver = new ChromeDriver();
}
else if(browser.equals("Firefox"))
{
    System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");
    driver = new FirefoxDriver();
}
else if(browser.equals("Edge"))
{
    System.setProperty("webdriver.edge.driver", "C:\\Program Files (x86)\\Edge Driver\\MicrosoftWebDriver.exe");
    driver = new EdgeDriver();
}
else if(browser.equals("IE11"))
{
    System.setProperty("webdriver.ie.driver", "C:\\Program Files (x86)\\Internet Explorer\\IEDriverServer32.exe");
    driver = new InternetExplorerDriver();
}
}

And on the cucumber side I use a scenario outline I just use a data table for browser grabbing:

|Browser|
|Chrome|
|FireFox|
|Edge|
|IE11|
Brian
  • 5,069
  • 7
  • 37
  • 47
potatocode
  • 13
  • 1
  • 6
  • Yikes, you really don't want to carry around a different `driver` variable for each browser. When you create your driver instance, declare that variable as `WebDriver`. Then you can use it no matter the browser you are driving. This will also eliminate the need for different waits for each driver instance too. – JeffC Mar 21 '17 at 19:40
  • I would also suggest that you not pass around `String`s as locators but instead pass around `By`s. For example, `public void unlinkUser(By socialLoginLocator)` and then when you click it, `driver.findElement(socialLoginLocator).click()`. – JeffC Mar 21 '17 at 19:41
  • @JeffC not quite sure what you mean. I'm only using two variables and I create the instance upon test execution in an Before annotation: Before public void websiteInitialization() { System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"); chromeDriver = new ChromeDriver(); System.setProperty("webdriver.ie.driver", "C:\\Program Files (x86)\\Internet Explorer\\IEDriverServer32.exe"); ieDriver = new InternetExplorerDriver(); } – potatocode Mar 22 '17 at 11:21
  • The markdown formatting doesn't seem to be working for me. I'm also using a scenario outline data table that has the locators, so if they were to ever change I only change them once in the data table and leave the step definitions alone. – potatocode Mar 22 '17 at 11:31
  • Additionally I declare globally as the start up to the step definition WebDriver ieDriver = null; – potatocode Mar 22 '17 at 11:35
  • You should have a `WebDriver` variable declared in the class that you store the instance in once it's created and then use that throughout the script. You should name it `driver` or something generic. It's confusing when you have two variables that are both `WebDriver` instances called `ieDriver` and `chromeDriver`. You don't need both, just pick one and use it. You should have a function to create the instance like what I've described here: http://stackoverflow.com/a/39382630/2386774. – JeffC Mar 22 '17 at 13:32
  • @JeffC I'm still trying to figure out how that switch statement works. Wouldn't that create a single instance of a firefox driver or chromedriver and you would have variable conflict? So lets say I wanted to open firefox, IE, opera, and chrome, and I can do all of that with a single variable once you call it? And then I just have some kind of driver control like driver.get("https://www.google.com/") and it would then go to google on all browsers you've created? – potatocode Mar 23 '17 at 16:12
  • It would and you want that. You would do that per run. In a given run, you will only use a single browser... at least that's how you should set up your tests. – JeffC Mar 23 '17 at 17:41
  • @JeffC thanks for the tip. I kept thinking that switch statement was creating parallel browser tests and not individual ones, that's why I was getting confused. I modified the framework to only use one browser at a time, but in a way that allows me to quickly swap development for each browser. – potatocode Mar 23 '17 at 20:44
  • I can also see the benefit of using 'By's instead of 'String' so I'll be doing that conversion over next. – potatocode Mar 23 '17 at 21:04
  • Exactly. If you pass `String`, you have to either tell the function whether it's `By.Id()` or whatever or guess (yuck). If you pass a `By`, all that is taken care of. – JeffC Mar 23 '17 at 22:35