简体   繁体   English

如何在 android 喷气背包撰写测试中断言文本不包含特定字符?

[英]how to assert that text not contains specific characters in android jetpack compose testing?

I'm trying to write some test cases for my compose functions.我正在尝试为我的撰写功能编写一些测试用例。 I have an outlined Text field with a maximum value of 16 characters.我有一个最大值为 16 个字符的概述文本字段。 So I want to test this feature.所以我想测试这个功能。 Here is the test:这是测试:

    @Test
    fun checkMaxTaxCodeLength_16Character() {
        val taxCode = composeRule.onNodeWithTag(testTag = AUTHENTICATION_SCREEN_TAX_CODE_EDIT_TEXT)
        for (i in 'A'..'Z')
            taxCode.performTextInput(i.toString())
        taxCode.assertTextEquals("ABCDEFGHIJKLMNOP")
    }

But although I can see the input is correct, the test fails, and it seems assertTextEquals doesn't work correctly.但是虽然我可以看到输入是正确的,但测试失败了,而且 assertTextEquals 似乎无法正常工作。 So:所以:

  • first of all, what am I doing wrong?首先,我做错了什么?
  • Second, is there any way to, instead of checking the equality, check the text does not contain specific characters?其次,有什么方法可以检查文本是否不包含特定字符,而不是检查相等性?

here is the code of text field:这是文本字段的代码:

                OutlinedTextField(
                    value = state.taxCode,
                    maxLines = 1,
                    onValueChange = { string ->
                        viewModel.onEvent(
                            AuthenticationEvent.TaxCodeChanged(string)
                        )
                    },
                    label = {
                        Text(text = stringResource(id = R.string.tax_code))
                    },
                    modifier = Modifier
                        .fillMaxWidth()
                        .testTag(TestingConstant.AUTHENTICATION_SCREEN_TAX_CODE_EDIT_TEXT)
                )

The maximum length is handled in the view model.最大长度在视图 model 中处理。 If the user adds more characters than 16, the view model won't update the state and keep the old value.如果用户添加的字符数超过 16 个,则视图 model 不会更新 state 并保留旧值。

Ok, still, the problem is open, but I achieved what I wanted another way.好的,问题仍然存在,但我以另一种方式实现了我想要的。 I used semantic nodes to get what is in edit text and compared it with what it should be:我使用语义节点来获取编辑文本中的内容,并将其与应有的内容进行比较:

    @Test
    fun checkMaxTaxCodeLength_16Character() {
        val taxCode = composeRule.onNodeWithTag(testTag = AUTHENTICATION_SCREEN_TAX_CODE_EDIT_TEXT)
        for (i in 'A'..'Z')
            taxCode.performTextInput(i.toString())
        for ((key,value) in taxCode.fetchSemanticsNode().config)
            if (key.name =="EditableText")
                assertEquals("ABCDEFGHIJKLMNOP",value.toString())
    }

first of all, what am I doing wrong?首先,我做错了什么?

assertTextEquals() takes the value of Text and EditableText in your semantics node combines them and then does a check against the values you pass in. The order does not matter, just make sure to pass in the value of the Text as one of the arguments. assertTextEquals()在语义节点中将TextEditableText的值组合起来,然后检查您传入的值。顺序无关紧要,只需确保将Text的值作为 arguments 之一传入.

     val mNode = composeTestRule.onNodeWithText("Email")) 
     mNode.performTextInput("test@mail.com")
     mNode.assertTextEquals("Email", "test@mail.com")

Please note the text Email is the label for the textfield composable.请注意文本Email是可组合文本字段的 label。

To get the semantic information about your nodes you can have要获取有关您的节点的语义信息,您可以拥有

    @Test
    fun print_semantics_tree() {
        composeTestRule.onRoot(useUnmergedTree = true).printToLog(TAG)
    }

For the TAG you can use any string.对于TAG ,您可以使用任何字符串。 After running the above test you can search the logcat with the specified TAG .运行上述测试后,您可以使用指定的TAG搜索 logcat。 You should see something like你应该看到类似的东西

    |-Node #3 at (l=155.0, t=105.0, r=925.0, b=259.0)px
                                                                                                        
    | Focused = 'false'
                                                                                                        
    | ImeAction = 'Default'
                                                                                                        
    | EditableText = 'test@mail.com'
                                                                                                        
    | TextSelectionRange = 'TextRange(0, 0)'
                                                                                                        
    | Text = '[Email]'
                                                                                                        
    | Actions = [RequestFocus, GetTextLayoutResult, SetText, SetSelection, 
                 OnClick, OnLongClick, PasteText]

Please note you can also obtain the semantics node object with an index operation rather than iterating through all the values.请注意,您还可以通过索引操作而不是遍历所有值来获取语义节点 object。

   val value = fetchSemanticsNode().config[EditableText]

   assertEquals("test@mail.com", value.toString())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM