How to access the element in a pop-up window in JavaScript?

In a webpage not created by me, there are many buttons in it.

I need to do the following tasks:

  1. Click one button(class is test) in the page, a new window will pop-up.
  2. Click the button(class is dotest) in the new pop-up window.
  3. Repeat step 1 to 2 until all the buttons in the page have been clicked.

I want to use JavaScript in Chrome console to automate the above task.

Below is my code:

var buttons = document.getElementsByClassName('test');

for (button of buttons) {
  button.click();
}

When button.click() is invoked, the pop-up window will appear. However, since the window is not created by me, how can I access the button(class dotest) in it(like this one)