1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64

headless / test / headless_client_browsertest.cc [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>

#include "base/functional/bind.h"
#include "content/public/test/browser_test.h"
#include "headless/test/headless_browser_test.h"
#include "headless/test/headless_browser_test_utils.h"
#include "headless/test/headless_devtooled_browsertest.h"
#include "testing/gtest/include/gtest/gtest.h"

using simple_devtools_protocol_client::SimpleDevToolsProtocolClient;

namespace headless {

class HeadlessClientBrowserTest : public HeadlessDevTooledBrowserTest {
 public:
  HeadlessClientBrowserTest() = default;

 private:
  void RunDevTooledTest() override {
    browser_devtools_client_.SendCommand(
        "Target.createTarget", Param("url", "about:blank"),
        base::BindOnce(&HeadlessClientBrowserTest::AttachToTarget,
                       base::Unretained(this)));
  }

  void AttachToTarget(base::Value::Dict result) {
    base::Value::Dict params;
    params.Set("targetId", DictString(result, "result.targetId"));
    params.Set("flatten", true);
    browser_devtools_client_.SendCommand(
        "Target.attachToTarget", std::move(params),
        base::BindOnce(&HeadlessClientBrowserTest::CreateSession,
                       base::Unretained(this)));
  }

  void CreateSession(base::Value::Dict result) {
    session_client_ = browser_devtools_client_.CreateSession(
        DictString(result, "result.sessionId"));

    session_client_->SendCommand(
        "Runtime.evaluate", Param("expression", "window.location.href"),
        base::BindOnce(&HeadlessClientBrowserTest::FinishTest,
                       base::Unretained(this)));
  }

  void FinishTest(base::Value::Dict result) {
    EXPECT_THAT(result, DictHasValue("result.result.value", "about:blank"));
    session_client_.reset();
    FinishAsynchronousTest();
  }

 private:
  std::unique_ptr<SimpleDevToolsProtocolClient> session_client_;
};

IN_PROC_BROWSER_TEST_F(HeadlessClientBrowserTest, FlatProtocolAccess) {
  RunTest();
}

}  // namespace headless