Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EarlGrey() has wrong line number for page objects #95

Open
bootstraponline opened this issue May 23, 2016 · 5 comments
Open

EarlGrey() has wrong line number for page objects #95

bootstraponline opened this issue May 23, 2016 · 5 comments

Comments

@bootstraponline
Copy link
Contributor

bootstraponline commented May 23, 2016

In EarlGrey.swift, EarlGrey is defined as:

public func EarlGrey() -> EarlGreyImpl! {
  return EarlGreyImpl.invokedFromFile(#file, lineNumber: #line)
}

I define a login page object:

public class LoginPage {

    //# MARK: - Login Page Elements

    private var domainField: GREYElementInteraction {
        return EarlGrey().selectElementWithMatcher(grey_accessibilityID("domain_field"))
    }

which is then used:

func assertPageObjects() {
    domainField.assertWithMatcher(grey_sufficientlyVisible())
}

The line number is wrong because EarlGrey() sets it to be on the EarlGrey().selectElementWithMatcher... line.

I've had to update EarlGrey.swift like this:

//# MARK: - Custom EarlGrey file/line

extension EarlGreyImpl {
    // Use @nonobjc to fix A declaration cannot be both 'final' and 'dynamic' error
    @nonobjc public static var file: String = ""
    @nonobjc public static var line: UInt = 0

    public static func setFromFile(file: String, _ line: UInt) {
        self.file = file
        self.line = line
    }
}

public func grey_invokedFromFile(file: String, _ line: UInt) {
    EarlGreyImpl.setFromFile(file, line)
}

public func EarlGrey() -> EarlGreyImpl! {
  return EarlGreyImpl.invokedFromFile(EarlGreyImpl.file, lineNumber: EarlGreyImpl.line)
}

and update the page object:

func assertPageObjects(file: String = #file, _ line: UInt = #line) {
    grey_invokedFromFile(file, line)

    domainField.assertWithMatcher(grey_sufficientlyVisible())
}

now the line number is correctly identified in the test. Thoughts on how to best update EarlGrey.swift to handle this use case?

@khandpur
Copy link
Collaborator

This particular feature is about highlighting the EarlGrey invocation in the test code which led to the failure. This is an interesting use case and will fail the same way for the Objc version as well. Even though Objc can rely on macro expansion to include file and line# alongside the original method calls it would require us to define a macro for each top-level API and sub-APIs which can be called from those top-level APIs. Perhaps we need to rethink this.

@tirodkar
Copy link
Collaborator

This seems to be fixed by the above PR. @bootstraponline please close this or update it in case this is still present.

@bootstraponline
Copy link
Contributor Author

// Use to report errors on the correct file/line
public func grey_invokedFromFile(_ file:StaticString, _ line:UInt) {
  EarlGreyImpl.invoked(fromFile: file.description, lineNumber: line)
}

I still have to use ^ throughout the code to get accurate file & line numbers. If that's the best we can do for Swift, then this issue is safe to close.

@tirodkar
Copy link
Collaborator

tirodkar commented Mar 1, 2017

@bootstraponline sorry, but how does this affect your final API? Do you have to add grey_invokedFromFile to each file that you add? Are you still able to do EarlGrey.select(...) ?

@bootstraponline
Copy link
Contributor Author

bootstraponline commented Mar 1, 2017

It looks like this using the page object pattern.

// swift 3
private static var loginButton: GREYElementInteraction {
  return EarlGrey.select(elementWithMatcher: grey_accessibilityID("login_button"))
}

static func tapLoginButton(_ file: StaticString = #file, _ line: UInt = #line) {
  grey_invokedFromFile(file, line)

  loginButton.perform(grey_tap())
}

func testLoginPage_loginSuccessful() {
  loginPage.tapLoginButton()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants