简体   繁体   English

编写 Espresso 测试时未找到 R.id

[英]R.id not found when writing Espresso test

I tried writing a small test to try Espresso.我尝试编写一个小测试来尝试 Espresso。

package com.mycompany.myapp.somemodule

import com.mycompany.myapp.R
import other.uselful.imports

@RunWith(AndroidJUnit4::class)
@LargeTest
class DemoTest {

    @get:Rule
    var startActivity: ActivityTestRule<StartActivity> = ActivityTestRule(StartActivity::class.java)


    @Test
    fun aDemoTest() {
        onView(withId(R.id.theElementId))
                .check(matches(isClickable()))
    }
}

Android Studio doesn't show any error. Android Studio 没有显示任何错误。 if I ctrl+click on R.id.theElementID , it finds it in the appropriate layout file.如果我 ctrl+click R.id.theElementID ,它会在适当的布局文件中找到它。 However when I try to run, I get a compile error:但是,当我尝试运行时,出现编译错误:

Unresolved reference: id未解析的参考:id

How can I solve this?我该如何解决这个问题?

I bumped into this issue.我碰到了这个问题。 Spent 4 hours investigating, and no luck finding anything useful on the internet.花了 4 个小时进行调查,但没有在互联网上找到任何有用的东西。

So, I'm hoping that the information below may help someone.所以,我希望下面的信息可以帮助某人。

Short answer:简短的回答:

  1. clean the project清理项目
  2. execute gradlew :app:build from the command line / Terminal, it may show a build error which leads to the root cause从命令行/终端执行gradlew :app:build ,它可能会显示导致根本原因的构建错误

The longer answer:更长的答案:

I did the following:我做了以下事情:

  1. I hit "Invalidate caches and restart" from Android Studio我从 Android Studio 中点击了“使缓存无效并重新启动”
  2. I've executed gradlew :app:build from the command line我已经从命令行执行了gradlew :app:build

The second step printed an error message that the BuildConfig was duplicated.第二步打印了一条错误消息,指出 BuildConfig 被复制。 The reason for this issue was, that I had 2 modules (android-app and android-library) with slightly different package names (I made a typo when creating the second one).这个问题的原因是,我有 2 个模块(android-app 和 android-library),它们的包名称略有不同(我在创建第二个模块时打错了)。 This led to the failed build from the command line.这导致从命令行构建失败。 After modifying the 2nd module's package name to be the same as the 1st one, the issue was solved.将第二个模块的包名修改为与第一个相同后,问题解决。

I didn't receive the "R.id not found" error.我没有收到“R.id not found”错误。 So, I could continue to write the UI tests, which worked :-)所以,我可以继续编写 UI 测试,这很有效:-)

Happy coding!快乐编码!

guess the actual problem is, that the activity had not been started.猜测实际问题是,该活动尚未开始。

R.id.theElementId might be an invalid resource descriptor. R.id.theElementId可能是无效的资源描述符。

try R.id.the_element_id instead.尝试R.id.the_element_id代替。

also, add @UiThreadTest annotation ...另外,添加@UiThreadTest注释...

Short answer: Try remove any incorrect import android.R statement.简短回答:尝试删除任何不正确的import android.R语句。

Long answer I also have faced this issue.长答案我也遇到过这个问题。

In my case, in test class, I was importing android.R package due to referring some incorrect resource id previously ( R.id.progress ) which belonged to android.R package.在我的情况下,测试类,我是进口android.R包,由于涉及一些不正确的资源ID前面( R.id.progress ),它属于android.R包。

I removed that reference as well ans the import statement ie import android.R from the test class and the issue resolved.我删除了该引用以及导入语句,即从测试类中import android.R并解决了问题。 Then source recognized the R.id.etPassword resource id of my layout file.然后 source 识别出我的布局文件的R.id.etPassword资源 ID。

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

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