Reputation: 69
I am using XCUITest to do AppleTV UI testing using Xcode 7.3.
It has XCUIElement
same like WebElement
. But is there any similar kind of getText()
method for XCUIElement
,which is present in Selenium WebElement.
Basically i want to capture text from the XCUIElement
.
E.g:
let ele:XCUIElement = XCUIApplication().collectionViews.cells[
"Sign In to Activate"].
So is there anyway to get the value : "Sign In to Activate" from the element. Any Help ?
Upvotes: 0
Views: 3003
Reputation: 7659
Once you have a staticText XCUIElement, you can get the text using the label
property.
let app = XCUIApplication()
let textElement = app.staticTexts["myLabelIdentifier"]
let text = textElement.label
You can't get the text of a cell because a cell is not a staticText (UILabel) object. If your cell contains a UILabel (text) then you'll need to locate that object first.
You can get all the text elements on the screen by doing app.staticTexts
- you'll then need to narrow the list of text elements down to just the one you're interested in. To do this, add an accessibilityIdentifier
to the UILabel in your app's code.
label.accessibilityIdentifier = "myLabelIdentifier" // This can be whatever String you like
If you have multiple cells where your label is reused, you'll also need to be able to identify the cell you're interested in too. For example, if you want to find the text element inside the third cell:
let app = XCUIApplication()
let cell = app.cells.elementBoundByIndex(2)
let textElement = cell.staticTexts["myLabelIdentifier"]
let text = textElement.label
Upvotes: 8
Reputation: 17008
With UI Testing you might have to think about your problem a little differently. Instead of trying to read a value of a known element, check if the element exists at all.
In practice, this means to check that a label with your expected value appears in your web view. So, if you are waiting for the "Sign In to Activate" label to appear you can assert it via the following:
let app = XCUIApplication()
let signInLabel = app.staticTexts["Sign In to Activate"]
XCTAssert(signInLabel.exists)
Or, if the element is actually a link, access it via links
, like so:
let signInLink = app.links["Sign In to Activate"]
XCTAssert(signInLink.exists)
Upvotes: 0